OLDataProvider.m 560 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094
  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. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. #import "ImageUtils.h"
  22. #import "RAConvertor.h"
  23. @interface OLDataProvider ()
  24. @end
  25. @implementation OLDataProvider
  26. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  27. {
  28. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  29. NSString* ret=@"No Price.";
  30. sqlite3_stmt * statement;
  31. // int count=0;
  32. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  33. {
  34. if (sqlite3_step(statement) == SQLITE_ROW)
  35. {
  36. char *price = (char*)sqlite3_column_text(statement, 0);
  37. if(price==nil)
  38. price="";
  39. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  40. double dp= [nsprice doubleValue];
  41. ret=[NSString stringWithFormat:@"%.2f",dp];
  42. }
  43. sqlite3_finalize(statement);
  44. }
  45. return ret;
  46. // // [iSalesDB close_db:db];
  47. //
  48. // return nil;
  49. //// if(dprice==DBL_MAX)
  50. //// ret= nil;
  51. //// else
  52. //// ret= [NSNumber numberWithDouble:dprice];
  53. //// return ret;
  54. }
  55. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  56. {
  57. NSString* ret= nil;
  58. NSString *sqlQuery = nil;
  59. // 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
  60. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  61. sqlite3_stmt * statement;
  62. // int count=0;
  63. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  64. {
  65. if (sqlite3_step(statement) == SQLITE_ROW)
  66. {
  67. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  68. if(imgurl==nil)
  69. imgurl="";
  70. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  71. ret=nsimgurl;
  72. }
  73. sqlite3_finalize(statement);
  74. }
  75. else
  76. {
  77. ret=@"";
  78. }
  79. // [iSalesDB close_db:db];
  80. // DebugLog(@"data string: %@",ret );
  81. return ret;
  82. }
  83. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  84. {
  85. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  86. //生成portfolio pdf需要的数据
  87. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  88. data[@"npd_url"]=COMPANY_WEB;
  89. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  90. [formatter setDateFormat:@"MMMM yyyy"];
  91. NSString* date = [formatter stringFromDate:[NSDate date]];
  92. data[@"create_month"]=date;
  93. data[@"company_name"]=COMPANY_FULL_NAME;
  94. data[@"catalog_name"]=params[@"catalog_name"];
  95. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  96. data[@"TOTAL_PAGE"]=0;
  97. data[@"CURRENT_PAGE"]=0;
  98. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  99. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  100. bool show_stockout=[params[@"show_stockout"] boolValue];
  101. NSString *sql = nil;
  102. // v1.90 more color
  103. NSString *group_by = @"";
  104. if(values==nil)
  105. {
  106. if (show_group_by) {
  107. group_by = @"group by b.product_group ";
  108. }
  109. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in,b.more_color from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id %@ order by a.name",params[@"product_ids"],params[@"product_ids"],group_by];
  110. }
  111. else
  112. {
  113. if (show_group_by) {
  114. group_by = @"group by product_group ";
  115. }
  116. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in,more_color from product where product_id in(%@) %@ order by name",params[@"product_ids"],group_by];
  117. }
  118. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  119. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  120. sqlite3 *db = [iSalesDB get_db];
  121. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  122. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  123. int product_id = sqlite3_column_int(stmt, 0);
  124. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  125. NSString *name = [self textAtColumn:1 statement:stmt];
  126. NSString *description = [self textAtColumn:2 statement:stmt];
  127. double price = sqlite3_column_double(stmt, 3);
  128. NSString *s_price = [self textAtColumn:3 statement:stmt];
  129. double discount = sqlite3_column_double(stmt,4);
  130. int qty = sqlite3_column_int(stmt, 5);
  131. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  132. int is_percentage = sqlite3_column_int(stmt, 6);
  133. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  134. int item_id = sqlite3_column_int(stmt, 7);
  135. // int fashion_id = sqlite3_column_int(stmt, 8);
  136. NSString *line_note = [self textAtColumn:9 statement:stmt];
  137. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  138. double percent = sqlite3_column_double(stmt, 10);
  139. // int portfolio_id = sqlite3_column_int(stmt, 11);
  140. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  141. int availability = sqlite3_column_int(stmt, 12);
  142. NSString *color = [self textAtColumn:13 statement:stmt];
  143. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  144. NSString *demension = [self textAtColumn:15 statement:stmt];
  145. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  146. NSString *material = [self textAtColumn:17 statement:stmt];
  147. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  148. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  149. double volume = sqlite3_column_double(stmt, 19);
  150. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  151. double weight = sqlite3_column_double(stmt, 20);
  152. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  153. int model_set = sqlite3_column_int(stmt, 21);
  154. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  155. double load_ability = sqlite3_column_double(stmt, 22);
  156. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  157. NSString *assembling = [self textAtColumn:24 statement:stmt];
  158. NSString *made_in = [self textAtColumn:25 statement:stmt];
  159. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  160. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  161. float bprice=0;
  162. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  163. for(int b=0;b< [bundle[@"count"] intValue];b++)
  164. {
  165. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  166. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  167. }
  168. if(gprice!=nil)
  169. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  170. if(s_percent.length==0||is_percentage==0)
  171. {
  172. percent=100.0;
  173. if([s_qty isEqualToString:@"null"])
  174. qty=availability;
  175. }
  176. else
  177. {
  178. qty=availability;
  179. }
  180. qty=qty*percent/100+0.5;
  181. for(int i=0;i<[values[@"count"] intValue];i++)
  182. {
  183. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  184. if([valueitem[@"product_id"] intValue]==product_id)
  185. {
  186. line_note=valueitem[@"line_note"];
  187. qty=[valueitem[@"available_qty"] intValue];
  188. if([[valueitem allKeys] containsObject:@"available_qty"])
  189. s_qty=[NSString stringWithFormat:@"%d",qty];
  190. else
  191. s_qty=@"null";
  192. if(valueitem[@"regular_price"]!=nil)
  193. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  194. s_price=valueitem[@"special_price"];
  195. price=[valueitem[@"special_price"] floatValue];
  196. discount=0;
  197. // v1.90
  198. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  199. }
  200. }
  201. if(is_percentage==0&&qty==0&&!show_stockout)
  202. {
  203. return;
  204. }
  205. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  206. {
  207. return;
  208. }
  209. NSString* set_price=@"";
  210. if([params[@"entered_price"] boolValue])
  211. {
  212. if (s_price==nil ) {
  213. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  214. }
  215. else
  216. {
  217. if(price*(1-discount/100.0)!=gprice.floatValue)
  218. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  219. }
  220. }
  221. NSString* get_price=@"";
  222. {
  223. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  224. // DebugLog(@"price time interval");
  225. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  226. if(gprice==nil)
  227. get_price=@"Price:No Price.";
  228. else
  229. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  230. }
  231. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  232. // if ([qty_null isEqualToString:@"null"]) {
  233. // qty=availability;
  234. // }
  235. //
  236. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  237. // if ([qty_percent_null isEqualToString:@"null"]) {
  238. // percentage=1;
  239. // }
  240. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  241. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  242. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  243. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  244. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  245. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  246. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  247. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  248. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  249. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  250. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  251. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  252. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  253. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  254. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  255. if([params[@"availability"] boolValue]==false)
  256. str_availability=@"";
  257. else
  258. {
  259. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  260. str_availability=@"<b>Availability:</b> In Production";
  261. }
  262. if([params[@"color"] boolValue]==false || color.length==0 )
  263. str_color=@"";
  264. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  265. str_model_set=@"";
  266. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  267. str_legcolor=@"";
  268. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  269. str_demension=@"";
  270. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  271. str_seat_height=@"";
  272. if([params[@"material"] boolValue]==false || material.length==0 )
  273. str_meterial=@"";
  274. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  275. str_box_dim=@"";
  276. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  277. str_volume=@"";
  278. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  279. str_weight=@"";
  280. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  281. str_load_ability=@"";
  282. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  283. str_fabric_content=@"";
  284. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  285. str_assembling=@"";
  286. if([params[@"made"] boolValue]==false || made_in.length==0 )
  287. str_made_in=@"";
  288. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  289. str_line_note=@"";
  290. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  291. [arr_detail addObject:str_availability];
  292. [arr_detail addObject:str_color];
  293. [arr_detail addObject:str_model_set];
  294. [arr_detail addObject:str_legcolor];
  295. [arr_detail addObject:str_demension];
  296. [arr_detail addObject:str_seat_height];
  297. [arr_detail addObject:str_meterial];
  298. [arr_detail addObject:str_box_dim];
  299. [arr_detail addObject:str_volume];
  300. [arr_detail addObject:str_weight];
  301. [arr_detail addObject:str_load_ability];
  302. [arr_detail addObject:str_fabric_content];
  303. [arr_detail addObject:str_assembling];
  304. [arr_detail addObject:str_made_in];
  305. [arr_detail addObject:str_line_note];
  306. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  307. NSString* detail =[RAConvertor arr2string:arr_detail separator:@" " trim:true];
  308. //model image;
  309. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  310. // qr image
  311. NSString* qrpath=nil;
  312. if([params[@"show_barcode"] boolValue])
  313. {
  314. NSString* temp = NSTemporaryDirectory();
  315. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  316. qrpath=[temp stringByAppendingPathComponent:filename];
  317. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  318. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  319. }
  320. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  321. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  322. item[@"img"]=img_path;
  323. item[@"img_qr"]=qrpath;
  324. item[@"special_price"]=set_price;
  325. item[@"price"]=get_price;
  326. item[@"name"]=name;
  327. item[@"description"]=description;
  328. item[@"detail"]=detail;
  329. item[@"img"]=img_path;
  330. if (show_group_by && more_color_int != 0) {
  331. item[@"more_color"] = more_color_path;
  332. }
  333. // @{
  334. // //@"linenotes": line_note,
  335. //// @"product_id": product_id_string,
  336. //// @"available_qty": @(qty),
  337. //// @"available_percent" : @(percent),
  338. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  339. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  340. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  341. // @"": ,
  342. // @"": ,
  343. // @"": ,
  344. // @"": ,
  345. // @"": ,
  346. // @"": ,
  347. // @"":
  348. // }.mutableCopy;
  349. // if (percentage) {
  350. // [item removeObjectForKey:@"available_qty"];
  351. // } else {
  352. // [item removeObjectForKey:@"available_percent"];
  353. // }
  354. //
  355. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  356. // if ([qty_null isEqualToString:@"null"]) {
  357. // [item removeObjectForKey:@"available_qty"];
  358. // }
  359. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  360. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  361. }];
  362. [iSalesDB close_db:db];
  363. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  364. // [dic setValue:@"" forKey:@"email_content"];
  365. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  366. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  367. //
  368. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  369. // cell[@"count"]=[NSNumber numberWithInt:10];
  370. grid[@"cell0"]=dic;
  371. data[@"grid0"]=grid;
  372. return data;
  373. }
  374. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  375. {
  376. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  377. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  378. NSString *cache_folder=[paths objectAtIndex:0];
  379. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  380. BOOL bdir=NO;
  381. NSFileManager* fileManager = [NSFileManager defaultManager];
  382. if(PDF_DEBUG)
  383. {
  384. NSData *data = [NSData dataWithContentsOfFile:default_path];
  385. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  386. return ret;
  387. }
  388. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  389. {
  390. NSError * error=nil;
  391. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  392. {
  393. return nil;
  394. }
  395. }
  396. NSData *data = [NSData dataWithContentsOfFile:template_path];
  397. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  398. return ret;
  399. }
  400. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  401. {
  402. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  403. 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,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  404. DebugLog(@"offline_login sql:%@",sqlQuery);
  405. sqlite3_stmt * statement;
  406. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  407. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  408. {
  409. if (sqlite3_step(statement) == SQLITE_ROW)
  410. {
  411. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  412. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  413. int can_show_price = sqlite3_column_int(statement, 0);
  414. int can_see_price = sqlite3_column_int(statement, 1);
  415. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  416. if(contact_id==nil)
  417. contact_id="";
  418. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  419. int user_type = sqlite3_column_int(statement, 3);
  420. int can_cancel_order = sqlite3_column_int(statement, 4);
  421. int can_set_cart_price = sqlite3_column_int(statement, 5);
  422. int can_create_portfolio = sqlite3_column_int(statement, 6);
  423. int can_delete_order = sqlite3_column_int(statement, 7);
  424. int can_submit_order = sqlite3_column_int(statement, 8);
  425. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  426. int can_create_order = sqlite3_column_int(statement, 10);
  427. char *mode = (char*)sqlite3_column_text(statement, 11);
  428. if(mode==nil)
  429. mode="";
  430. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  431. char *username = (char*)sqlite3_column_text(statement, 12);
  432. if(username==nil)
  433. username="";
  434. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  435. int can_update_contact_info = sqlite3_column_int(statement, 13);
  436. char *first_name = (char*)sqlite3_column_text(statement, 14);
  437. if(first_name==nil)
  438. first_name="";
  439. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  440. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  441. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  442. // [header setValue:nscontact_id forKey:@"contact_id"];
  443. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  444. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  445. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  446. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  447. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  448. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  449. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  450. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  451. //
  452. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  453. //
  454. // [header setValue:nsusername forKey:@"username"];
  455. //
  456. // NSError* error=nil;
  457. //
  458. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  459. // [header setValue:statusFilter forKey:@"statusFilter"];
  460. //
  461. //
  462. //
  463. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  464. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  465. //
  466. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  467. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  468. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  469. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  470. //
  471. // [ret setObject:header forKey:@"header"];
  472. [ret setValue:nsfirst_name forKey:@"first_name"];
  473. }
  474. sqlite3_finalize(statement);
  475. }
  476. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  477. return ret;
  478. }
  479. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  480. {
  481. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  482. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  483. data[@"npd_url"]=COMPANY_WEB;
  484. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  485. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  486. NSString* date = [formatter stringFromDate:[NSDate date]];
  487. data[@"print_date"]=date;
  488. data[@"company_name"]=COMPANY_FULL_NAME;
  489. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  490. data[@"TOTAL_PAGE"]=0;
  491. data[@"CURRENT_PAGE"]=0;
  492. // [formatter setDateFormat:@"MM/dd/yyyy"];
  493. // date = [formatter stringFromDate:[NSDate date]];
  494. // data[@"creat_date"]=date;
  495. NSString* temp = NSTemporaryDirectory();
  496. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  497. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  498. UIImage* qrimg=[ImageUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  499. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  500. data[@"img_barcode"]=qrpath;
  501. data[@"so_id"]=order_code;
  502. data[@"company"]=COMPANY_FULL_NAME;
  503. // 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 so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  504. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  505. sqlite3 *db = [iSalesDB get_db];
  506. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  507. sqlite3_stmt * statement;
  508. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  509. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  510. row0[@"count"]=[NSNumber numberWithInt:1];
  511. datasource[@"row0"]=row0;
  512. NSString* print_status=@"";
  513. NSString* order_type=@"Offline Quote";
  514. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  515. {
  516. if (sqlite3_step(statement) == SQLITE_ROW)
  517. {
  518. int erpOrderStatus = sqlite3_column_int(statement, 0);
  519. int status = sqlite3_column_int(statement, 26);
  520. if(status==2)
  521. {
  522. switch (erpOrderStatus) {
  523. case 10:
  524. // order_type=@"Quoted Order";
  525. print_status=@"Quote";
  526. break;
  527. case 11:
  528. print_status=@"Booked";
  529. break;
  530. case 12:
  531. print_status=@"Picked";
  532. break;
  533. case 13:
  534. print_status=@"Shipped";
  535. break;
  536. case 14:
  537. print_status=@"Closed";
  538. break;
  539. case 15:
  540. print_status=@"Canceled";
  541. break;
  542. case 16:
  543. print_status=@"All";
  544. break;
  545. case 18:
  546. print_status=@"Processing";
  547. break;
  548. default:
  549. break;
  550. }
  551. }
  552. else{
  553. switch (status) {
  554. case 0:
  555. {
  556. print_status=@"Quote";
  557. break;
  558. }
  559. case 1:
  560. {
  561. print_status=@"Saved";
  562. break;
  563. }
  564. case 3:
  565. {
  566. //canceled
  567. print_status=@"Canceled";
  568. break;
  569. }
  570. default:
  571. break;
  572. }
  573. }
  574. NSString* sender_name = [self textAtColumn:1 statement:statement];
  575. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  576. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  577. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  578. NSString* sender_email = [self textAtColumn:5 statement:statement];
  579. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  580. NSString* billing_name = [self textAtColumn:7 statement:statement];
  581. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  582. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  583. if(billing_phone.length>0)
  584. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  585. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  586. if(billing_fax.length>0)
  587. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  588. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  589. [billto_arr0 addObject:billing_phone];
  590. [billto_arr0 addObject:billing_fax];
  591. NSString* billing_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  592. NSString* billing_email = [self textAtColumn:11 statement:statement];
  593. if(billing_email.length>0)
  594. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  595. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  596. if(billing_contact.length>0)
  597. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  598. NSString* receive_name = [self textAtColumn:12 statement:statement];
  599. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  600. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  601. if(receive_phone.length>0)
  602. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  603. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  604. if(receive_fax.length>0)
  605. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  606. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  607. [receive_arr0 addObject:receive_phone];
  608. [receive_arr0 addObject:receive_fax];
  609. NSString* receive_email = [self textAtColumn:16 statement:statement];
  610. NSString* receive_pf=[RAConvertor arr2string:billto_arr0 separator:@" " trim:true];
  611. if(receive_email.length>0)
  612. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  613. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  614. if(receive_contact.length>0)
  615. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  616. NSString* poNumber = [self textAtColumn:17 statement:statement];
  617. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  618. NSString* carrier = [self textAtColumn:19 statement:statement];
  619. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  620. NSString* terms = [self textAtColumn:21 statement:statement];
  621. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  622. NSString* port_destination = [self textAtColumn:23 statement:statement];
  623. NSString* etd = [self textAtColumn:24 statement:statement];
  624. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  625. if(sign_picpath.length==0)
  626. sign_picpath=nil;
  627. NSString* create_date = [self textAtColumn:27 statement:statement];
  628. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  629. NSString* general_notes=[self textAtColumn:33 statement:statement];
  630. NSString *customerID = [self textAtColumn:34 statement:statement];
  631. if (customerID.length == 0) {
  632. customerID = nil;
  633. }
  634. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  635. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  636. if(general_notes.length>0)
  637. {
  638. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  639. row2[@"item_0"]=@{@"g_notes":general_notes };
  640. row2[@"count"]=[NSNumber numberWithInt:1];
  641. datasource[@"row2"]=row2;
  642. }
  643. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  644. [billto_arr addObject:billing_ext];
  645. [billto_arr addObject:billing_pf];
  646. [billto_arr addObject:billing_contact];
  647. [billto_arr addObject:billing_email];
  648. NSString* billto=[RAConvertor arr2string:billto_arr separator:@"\r\n" trim:true];
  649. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  650. [receive_arr addObject:receive_ext];
  651. [receive_arr addObject:receive_pf];
  652. [receive_arr addObject:receive_contact];
  653. [receive_arr addObject:receive_email];
  654. NSString* receive=[RAConvertor arr2string:receive_arr separator:@"\r\n" trim:true];
  655. float shipping = sqlite3_column_double(statement, 28);
  656. float lift_gate_value = sqlite3_column_double(statement, 29);
  657. float handling_fee_value = sqlite3_column_double(statement, 30);
  658. data[@"company"]=sender_name;
  659. data[@"shipfrom_addr"]=sender_ext;
  660. data[@"shipfrom_phone"]=sender_phone;
  661. data[@"shipfrom_fax"]=sender_fax;
  662. data[@"shipfrom_email"]=sender_email;
  663. data[@"warehouse_name"]=warehouse_name;
  664. data[@"bill_to_name"]=billing_name;
  665. data[@"bill_to_ext"]=billto;
  666. data[@"ship_to_name"]=receive_name;
  667. data[@"ship_to_ext"]=receive;
  668. data[@"po_no"]=poNumber;
  669. data[@"rep"]=sales_rep;
  670. data[@"e_schdule"]=etd;
  671. data[@"sales_terms"]=sales_terms;
  672. data[@"port_destination"]=port_destination;
  673. data[@"terms"]=terms;
  674. data[@"carrier"]=carrier;
  675. data[@"vendor_no"]=vendor_no;
  676. data[@"customer_sign"]=sign_picpath;
  677. data[@"print_status"]=print_status;
  678. data[@"create_date"]=create_date;
  679. NSString *sql_cart=[ 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 ",order_code];;
  680. sqlite3_stmt * statement_cart;
  681. // NSDate *date1 = [NSDate date];
  682. int count=0;
  683. int cart_count=0;
  684. float credits=0;
  685. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  686. if ( dbresult== SQLITE_OK)
  687. {
  688. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  689. int row1count=0;
  690. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  691. {
  692. NSDate *row_date = [NSDate date];
  693. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  694. // int product_id = sqlite3_column_int(statement_cart, 0);
  695. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  696. int item_id = sqlite3_column_int(statement_cart, 7);
  697. double discount = sqlite3_column_double(statement_cart, 2);
  698. NSString* Price=nil;
  699. if(str_price==nil)
  700. {
  701. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  702. // NSDate *price_date = [NSDate date];
  703. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  704. // DebugLog(@"price time interval");
  705. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  706. if(price==nil)
  707. Price=@"No Price.";
  708. else
  709. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  710. }
  711. else
  712. {
  713. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  714. }
  715. float dunitprice=0;
  716. int item_count = sqlite3_column_int(statement_cart, 3);
  717. NSString* Amount=@"";
  718. if(![Price isEqualToString:@"No Price."])
  719. {
  720. dunitprice=[Price floatValue];
  721. dunitprice=dunitprice* (1-discount/100.0);
  722. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  723. credits+=dunitprice*item_count;
  724. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  725. }
  726. else
  727. {
  728. Price=@"";
  729. }
  730. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  731. // NSString *nsline_note=nil;
  732. // if(line_note!=nil)
  733. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  734. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  735. NSString *nsname=nil;
  736. if(name!=nil)
  737. nsname= [[NSString alloc]initWithUTF8String:name];
  738. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  739. NSString *nsdescription=nil;
  740. if(description!=nil)
  741. nsdescription= [[NSString alloc]initWithUTF8String:description];
  742. // int stockUom = sqlite3_column_int(statement_cart, 8);
  743. // int _id = sqlite3_column_int(statement_cart, 9);
  744. // NSDate *subtotal_date = [NSDate date];
  745. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  746. // DebugLog(@"subtotal_date time interval");
  747. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  748. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  749. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  750. // int carton=[bsubtotaljson[@"carton"] intValue];
  751. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  752. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  753. // NSDate *img_date = [NSDate date];
  754. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  755. // DebugLog(@"img_date time interval");
  756. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  757. itemjson[@"model"]=nsname;
  758. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  759. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  760. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  761. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  762. // itemjson[@"check"]=@"true";
  763. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  764. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  765. itemjson[@"unit_price"]=Price;
  766. itemjson[@"amount"]=Amount;
  767. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  768. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  769. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  770. // itemjson[@"note"]=nsline_note;
  771. // NSDate *date2 = [NSDate date];
  772. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  773. // DebugLog(@"model_bundle time interval");
  774. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  775. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  776. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  777. row1count++;
  778. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  779. for(int c=0;c<[combine[@"count"] intValue];c++)
  780. {
  781. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  782. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  783. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  784. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  785. credits+=combine_amount;
  786. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  787. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  788. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  789. row1count++;
  790. }
  791. count++;
  792. // DebugLog(@"row time interval");
  793. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  794. }
  795. row1[@"count"]=[NSNumber numberWithInt:row1count];
  796. datasource[@"row1"]=row1;
  797. data[@"table_model"]=datasource;
  798. sqlite3_finalize(statement_cart);
  799. }
  800. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  801. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  802. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  803. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  804. // if(credits>0)
  805. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  806. // else
  807. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  808. if(shipping>0)
  809. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  810. else
  811. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  812. if(lift_gate_value>0)
  813. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  814. else
  815. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  816. if(handling_fee_value>0)
  817. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  818. else
  819. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  820. float total = credits+shipping+lift_gate_value+handling_fee_value;
  821. if(total>0)
  822. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  823. else
  824. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  825. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  826. fee[@"row0"]=fee_row0;
  827. data[@"table_fee"]=fee;
  828. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  829. // ret[@"sign_url"] = sign_url;
  830. //
  831. // customer_contact = [self textAtColumn:52 statement:statement];
  832. // customer_email = [self textAtColumn:53 statement:statement];
  833. // customer_phone = [self textAtColumn:54 statement:statement];
  834. // customer_fax = [self textAtColumn:55 statement:statement];
  835. //
  836. //
  837. // int offline_edit=sqlite3_column_int(statement, 56);
  838. //
  839. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  840. //
  841. // char *soid = (char*)sqlite3_column_text(statement, 1);
  842. // if(soid==nil)
  843. // soid= "";
  844. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  845. // // so#
  846. // ret[@"so#"] = nssoid;
  847. //
  848. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  849. // if(poNumber==nil)
  850. // poNumber= "";
  851. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  852. //
  853. //
  854. //
  855. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  856. // if(create_time==nil)
  857. // create_time= "";
  858. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  859. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  860. //
  861. // int status = sqlite3_column_int(statement, 4);
  862. // int erpStatus = sqlite3_column_int(statement, 49);
  863. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  864. //
  865. // // status
  866. // if (status > 1 && status != 3) {
  867. // status = erpStatus;
  868. // } else if (status == 3) {
  869. // status = 15;
  870. // }
  871. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  872. // ret[@"order_status"] = nsstatus;
  873. //
  874. //
  875. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  876. // if(company_name==nil)
  877. // company_name= "";
  878. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  879. // // company name
  880. // ret[@"company_name"] = nscompany_name;
  881. //
  882. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  883. // if(customer_contact==nil)
  884. // customer_contact= "";
  885. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  886. //
  887. //
  888. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  889. // if(addr_1==nil)
  890. // addr_1="";
  891. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  892. //
  893. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  894. // if(addr_2==nil)
  895. // addr_2="";
  896. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  897. //
  898. //
  899. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  900. // if(addr_3==nil)
  901. // addr_3="";
  902. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  903. //
  904. //
  905. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  906. // if(addr_4==nil)
  907. // addr_4="";
  908. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  909. //
  910. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  911. // [arr_addr addObject:nsaddr_1];
  912. // [arr_addr addObject:nsaddr_2];
  913. // [arr_addr addObject:nsaddr_3];
  914. // [arr_addr addObject:nsaddr_4];
  915. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  916. //
  917. //
  918. // char *logist = (char*)sqlite3_column_text(statement, 11);
  919. // if(logist==nil)
  920. // logist= "";
  921. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  922. // if (status == -11 || status == 10 || status == 11) {
  923. // nslogist = [self textAtColumn:59 statement:statement];
  924. // };
  925. //
  926. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  927. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  928. // shipping = @"Shipping To Be Quoted";
  929. // } else {
  930. // shippingFee = sqlite3_column_double(statement, 12);
  931. // }
  932. //
  933. // // Shipping
  934. // ret[@"Shipping"] = shipping;
  935. //
  936. // int have_lift_gate = sqlite3_column_int(statement, 17);
  937. // lift_gate = sqlite3_column_double(statement, 13);
  938. // // Liftgate Fee(No loading dock)
  939. // if (!have_lift_gate) {
  940. // lift_gate = 0;
  941. // }
  942. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  943. // if (sqlite3_column_int(statement, 57)) {
  944. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  945. // }
  946. //
  947. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  948. // if(general_notes==nil)
  949. // general_notes= "";
  950. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  951. //
  952. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  953. // if(internal_notes==nil)
  954. // internal_notes= "";
  955. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  956. //
  957. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  958. // if(payment_type==nil)
  959. // payment_type= "";
  960. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  961. //
  962. //
  963. //
  964. // // order info
  965. // orderinfo = [self textFileName:@"order_info.html"];
  966. //
  967. //
  968. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  969. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  970. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  971. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  972. //
  973. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  974. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  975. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  976. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  977. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  978. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  979. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  980. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  981. //
  982. // NSString *payment = nil;
  983. // if([nspayment_type isEqualToString:@"Credit Card"])
  984. // {
  985. // payment = [self textFileName:@"creditcardpayment.html"];
  986. //
  987. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  988. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  989. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  990. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  991. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  992. // NSString *card_type = [self textAtColumn:42 statement:statement];
  993. // if (card_type.length > 0) { // 显示星号
  994. // card_type = @"****";
  995. // }
  996. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  997. // if (card_number.length > 0 && card_number.length > 4) {
  998. // for (int i = 0; i < card_number.length - 4; i++) {
  999. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  1000. // }
  1001. // } else {
  1002. // card_number = @"";
  1003. // }
  1004. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  1005. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  1006. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  1007. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  1008. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1009. // card_expiration = @"****";
  1010. // }
  1011. //
  1012. //
  1013. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1014. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1015. //
  1016. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1017. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1018. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1019. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1020. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1021. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1022. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1023. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1024. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1025. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1026. //
  1027. //
  1028. // }
  1029. // else
  1030. // {
  1031. // payment=[self textFileName:@"normalpayment.html"];
  1032. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1033. // }
  1034. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1035. //
  1036. // ret[@"result"]= [NSNumber numberWithInt:2];
  1037. //
  1038. // // more info
  1039. // moreInfo = [self textFileName:@"more_info.html"];
  1040. //
  1041. //
  1042. // // ShipToCompany_or_&nbsp
  1043. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1044. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1045. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1046. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1047. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1049. //
  1050. //
  1051. // // ShipFromCompany_or_&nbsp
  1052. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1054. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1056. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1058. //
  1059. //
  1060. // // FreightBillToCompany_or_&nbsp
  1061. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1062. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1063. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1064. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1065. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1066. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1067. //
  1068. //
  1069. // // MerchandiseBillToCompany_or_&nbsp
  1070. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1071. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1072. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1073. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1074. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1075. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1076. //
  1077. //
  1078. // // ReturnToCompany_or_&nbsp
  1079. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1080. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1081. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1082. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1083. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1084. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1085. // //
  1086. //
  1087. // // DebugLog(@"more info : %@",moreInfo);
  1088. //
  1089. // // handling fee
  1090. // handlingFee = sqlite3_column_double(statement, 33);
  1091. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1092. // if (sqlite3_column_int(statement, 58)) {
  1093. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1094. // }
  1095. // //
  1096. //
  1097. // // customer info
  1098. // customerID = [self textAtColumn:36 statement:statement];
  1099. //
  1100. // // mode
  1101. // ret[@"mode"] = appDelegate.mode;
  1102. //
  1103. // // model_count
  1104. // ret[@"model_count"] = @(0);
  1105. }
  1106. sqlite3_finalize(statement);
  1107. }
  1108. [iSalesDB close_db:db];
  1109. data[@"order_type"]=order_type;
  1110. /*
  1111. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1112. // "customer_email" = "Shui Hu";
  1113. // "customer_fax" = "";
  1114. // "customer_first_name" = F;
  1115. // "customer_last_name" = L;
  1116. // "customer_name" = ",da He Xiang Dong Liu A";
  1117. // "customer_phone" = "Hey Xuan Feng";
  1118. contactInfo[@"customer_phone"] = customer_phone;
  1119. contactInfo[@"customer_fax"] = customer_fax;
  1120. contactInfo[@"customer_email"] = customer_email;
  1121. NSString *first_name = @"";
  1122. NSString *last_name = @"";
  1123. if ([customer_contact isEqualToString:@""]) {
  1124. } else if ([customer_contact containsString:@" "]) {
  1125. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1126. first_name = [customer_contact substringToIndex:first_space_index];
  1127. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1128. }
  1129. contactInfo[@"customer_first_name"] = first_name;
  1130. contactInfo[@"customer_last_name"] = last_name;
  1131. ret[@"customerInfo"] = contactInfo;
  1132. // models
  1133. if (nssoid) {
  1134. __block double TotalCuft = 0;
  1135. __block double TotalWeight = 0;
  1136. __block int TotalCarton = 0;
  1137. __block double allItemPrice = 0;
  1138. 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];
  1139. sqlite3 *db1 = [iSalesDB get_db];
  1140. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1141. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1142. int product_id = sqlite3_column_int(stmt, 0);
  1143. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1144. int item_id = sqlite3_column_int(stmt, 7);
  1145. NSString* Price=nil;
  1146. if(str_price==nil)
  1147. {
  1148. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1149. if(price==nil)
  1150. Price=@"No Price.";
  1151. else
  1152. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1153. }
  1154. else
  1155. {
  1156. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1157. }
  1158. double discount = sqlite3_column_double(stmt, 2);
  1159. int item_count = sqlite3_column_int(stmt, 3);
  1160. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1161. NSString *nsline_note=nil;
  1162. if(line_note!=nil)
  1163. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1164. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1165. // NSString *nsname = nil;
  1166. // if(name!=nil)
  1167. // nsname= [[NSString alloc]initWithUTF8String:name];
  1168. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1169. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1170. // NSString *nsdescription=nil;
  1171. // if(description!=nil)
  1172. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1173. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1174. // int stockUom = sqlite3_column_int(stmt, 8);
  1175. // int _id = sqlite3_column_int(stmt, 9);
  1176. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1177. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1178. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1179. int carton=[bsubtotaljson[@"carton"] intValue];
  1180. TotalCuft += cuft;
  1181. TotalWeight += weight;
  1182. TotalCarton += carton;
  1183. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1184. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1185. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1186. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1187. itemjson[@"note"]=nsline_note;
  1188. itemjson[@"origin_price"] = Price;
  1189. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1190. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1191. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1192. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1193. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1194. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1195. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1196. if(itemjson[@"combine"] != nil)
  1197. {
  1198. // int citem=0;
  1199. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1200. for(int bc=0;bc<bcount;bc++)
  1201. {
  1202. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1203. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1204. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1205. subTotal += uprice * modulus * item_count;
  1206. }
  1207. }
  1208. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1209. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1210. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1211. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1212. allItemPrice += subTotal;
  1213. }];
  1214. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1215. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1216. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1217. // payments/Credits
  1218. // payments_and_credist = sqlite3_column_double(statement, 34);
  1219. payments_and_credist = allItemPrice;
  1220. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1221. // // total
  1222. // totalPrice = sqlite3_column_double(statement, 35);
  1223. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1224. } else {
  1225. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1226. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1227. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1228. // payments/Credits
  1229. payments_and_credist = 0;
  1230. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1231. }
  1232. // total
  1233. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1234. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1235. ret[@"order_info"]= orderinfo;
  1236. ret[@"more_order_info"] = moreInfo;
  1237. return [RAConvertor dict2data:ret];
  1238. */
  1239. DebugLog(@"debug sales order data:%@", [RAConvertor dict2string:data]);
  1240. return data;
  1241. }
  1242. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1243. {
  1244. NSString* template_file=nil;
  1245. switch([params[@"thumb"] intValue])
  1246. {
  1247. case 0:
  1248. template_file= @"so.json";
  1249. break;
  1250. case 1:
  1251. template_file= @"so_thumb.json";
  1252. break;
  1253. default:
  1254. template_file= @"so.json";
  1255. }
  1256. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1257. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1258. if(data[@"table_model"][@"row2"]==nil)
  1259. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1260. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1261. DebugLog(@"%@",file);
  1262. // return nil;
  1263. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1264. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1265. if (file) {
  1266. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1267. [dic setObject:file forKey:@"pdf_path"];
  1268. dic[@"isLocalFile"]=@"true";
  1269. return [RAConvertor dict2data:dic];
  1270. }
  1271. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1272. dic[@"isLocalFile"]=@"true";
  1273. return [RAConvertor dict2data:dic];
  1274. }
  1275. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1276. {
  1277. NSString* template_file=nil;
  1278. switch([params[@"pdf_style"] intValue])
  1279. {
  1280. case 0:
  1281. template_file= @"portfolio_2x3.json";
  1282. break;
  1283. case 1:
  1284. template_file= @"portfolio_3x2.json";
  1285. break;
  1286. default:
  1287. template_file= @"portfolio_3x2.json";
  1288. }
  1289. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1290. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1291. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1292. DebugLog(@"%@",file);
  1293. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1294. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1295. if (file) {
  1296. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1297. [dic setObject:file forKey:@"pdf_path"];
  1298. dic[@"isLocalFile"]=@"true";
  1299. return [RAConvertor dict2data:dic];
  1300. }
  1301. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1302. dic[@"isLocalFile"]=@"true";
  1303. return [RAConvertor dict2data:dic];
  1304. }
  1305. +(NSString*) get_offline_soid:(sqlite3*)db
  1306. {
  1307. NSString* soid=nil;
  1308. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1309. [formatter setDateFormat:@"yyMMdd"];
  1310. NSString* date = [formatter stringFromDate:[NSDate date]];
  1311. for(int i=1;i<999;i++)
  1312. {
  1313. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1314. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1315. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1316. if(count==0)
  1317. return soid;
  1318. }
  1319. return nil;
  1320. }
  1321. +(NSArray*) enumOfflineOrder
  1322. {
  1323. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1324. sqlite3 *db = [iSalesDB get_db];
  1325. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1326. sqlite3_stmt * statement;
  1327. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1328. if ( dbresult== SQLITE_OK)
  1329. {
  1330. while (sqlite3_step(statement) == SQLITE_ROW)
  1331. {
  1332. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1333. NSString* nsso_id=nil;
  1334. if(so_id!=nil)
  1335. {
  1336. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1337. }
  1338. [ret addObject:nsso_id];
  1339. }
  1340. sqlite3_finalize(statement);
  1341. }
  1342. [iSalesDB close_db:db];
  1343. return ret;
  1344. }
  1345. //+(void) uploadFile:(NSString*) file
  1346. //{
  1347. // NSData* data = [NSData dataWithContentsOfFile: file];
  1348. // UIApplication * app = [UIApplication sharedApplication];
  1349. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1350. //
  1351. //
  1352. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1353. //
  1354. //
  1355. //
  1356. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1357. //
  1358. // if(appDelegate.user!=nil)
  1359. // [params setValue:appDelegate.user forKey:@"user"];
  1360. // // if(appDelegate.contact_id!=nil)
  1361. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1362. // if(appDelegate.password!=nil)
  1363. // [params setValue:appDelegate.password forKey:@"password"];
  1364. //
  1365. //
  1366. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1367. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1368. // } error:nil];
  1369. //
  1370. //
  1371. //
  1372. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1373. //
  1374. // NSProgress *progress = nil;
  1375. //
  1376. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1377. //
  1378. //
  1379. //
  1380. // if (error) {
  1381. //
  1382. // NSString* err_msg = [error localizedDescription];
  1383. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1384. //
  1385. //
  1386. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1387. //
  1388. // DebugLog(@"data string: %@",str);
  1389. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1390. //
  1391. // } else {
  1392. // DebugLog(@"response ");
  1393. //
  1394. //
  1395. //
  1396. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1397. //
  1398. // // 再将NSData转为字符串
  1399. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1400. //
  1401. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1402. //
  1403. // DebugLog(@"data string: %@",jsonStr);
  1404. //
  1405. // NSDictionary* json = responseObject;
  1406. //
  1407. //
  1408. // if([[json valueForKey:@"result"] intValue]==2)
  1409. // {
  1410. //// NSString* img_url_down = json[@"img_url_aname"];
  1411. //// NSString* img_url_up = json[@"img_url"];
  1412. //
  1413. //
  1414. // }
  1415. // else
  1416. // {
  1417. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1418. // }
  1419. //
  1420. //
  1421. // }
  1422. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1423. // }];
  1424. //
  1425. //
  1426. //
  1427. //
  1428. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1429. // //
  1430. //
  1431. //
  1432. //
  1433. //
  1434. //
  1435. //
  1436. // [uploadTask resume];
  1437. //
  1438. //}
  1439. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1440. {
  1441. NSString* serial= [[NSUUID UUID] UUIDString];
  1442. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1443. NSString *cachefolder = [paths objectAtIndex:0];
  1444. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1445. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1446. NSFileManager* fileManager = [NSFileManager defaultManager];
  1447. BOOL bdir=YES;
  1448. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1449. {
  1450. NSError *error = nil;
  1451. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1452. if(!bsuccess)
  1453. DebugLog(@"Create UPLOAD folder failed");
  1454. }
  1455. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1456. ret[@"contact"]=[self prepareContact:serial];
  1457. ret[@"wishlist"]=[self prepareWishlist:serial];
  1458. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1459. ret[@"portfolio"] = [self preparePortfolio:serial];
  1460. ret[@"view_portfolio"] = [self preparePDF:serial];
  1461. NSString* str= [RAConvertor dict2string:ret];
  1462. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1463. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1464. NSError *error=nil;
  1465. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1466. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1467. ZipArchive* zip = [[ZipArchive alloc] init];
  1468. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1469. for(int i=0;i<arr_files.count;i++)
  1470. {
  1471. NSString* file=arr_files[i];
  1472. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1473. }
  1474. if( ![zip CloseZipFile2] )
  1475. {
  1476. zippath = @"";
  1477. }
  1478. ret[@"file"]=zippath;
  1479. return ret;
  1480. }
  1481. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1482. {
  1483. if(filename.length==0)
  1484. return false;
  1485. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1486. bool ret=false;
  1487. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1488. NSString *cachefolder = [paths objectAtIndex:0];
  1489. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1490. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1491. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1492. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1493. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1494. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1495. NSFileManager* fileManager = [NSFileManager defaultManager];
  1496. BOOL bdir=NO;
  1497. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1498. {
  1499. NSError *error = nil;
  1500. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1501. {
  1502. ret=false;
  1503. }
  1504. else
  1505. {
  1506. ret=true;
  1507. }
  1508. }
  1509. return ret;
  1510. }
  1511. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1512. {
  1513. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1514. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1515. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1516. NSString* where=@" 1=1";
  1517. if (ver!=nil) {
  1518. where=@"is_dirty=1";
  1519. }
  1520. sqlite3 *db = [iSalesDB get_db];
  1521. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1522. sqlite3_stmt * statement;
  1523. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1524. int count=0;
  1525. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1526. if ( dbresult== SQLITE_OK)
  1527. {
  1528. while (sqlite3_step(statement) == SQLITE_ROW)
  1529. {
  1530. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1531. int _id = sqlite3_column_int(statement, 0);
  1532. int product_id = sqlite3_column_int(statement, 1);
  1533. int item_id = sqlite3_column_int(statement, 2);
  1534. int qty = sqlite3_column_int(statement, 3);
  1535. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1536. NSString* nscreate_time=nil;
  1537. if(create_time!=nil)
  1538. {
  1539. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1540. }
  1541. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1542. NSString* nsmodify_time=nil;
  1543. if(modify_time!=nil)
  1544. {
  1545. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1546. }
  1547. int is_delete = sqlite3_column_int(statement, 6);
  1548. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1549. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1550. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1551. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1552. itemjson[@"create_time"]=nscreate_time;
  1553. itemjson[@"modify_time"]=nsmodify_time;
  1554. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1555. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1556. count++;
  1557. }
  1558. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1559. sqlite3_finalize(statement);
  1560. }
  1561. ret[@"count"]=[NSNumber numberWithInt:count ];
  1562. [iSalesDB close_db:db];
  1563. return ret;
  1564. }
  1565. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1566. {
  1567. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1568. UIApplication * app = [UIApplication sharedApplication];
  1569. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1570. 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,o.customer_cid from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,so_no from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id left join offline_order as o on c.so_no = o.so_id;",orderCode ];
  1571. // 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 ];
  1572. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1573. DebugLog(@"offline_login sql:%@",sqlQuery);
  1574. sqlite3_stmt * statement;
  1575. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1576. int count=0;
  1577. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1578. if ( dbresult== SQLITE_OK)
  1579. {
  1580. while (sqlite3_step(statement) == SQLITE_ROW)
  1581. {
  1582. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1583. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1584. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1585. int product_id = sqlite3_column_int(statement, 0);
  1586. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1587. int item_id = sqlite3_column_int(statement, 7);
  1588. NSString* Price=nil;
  1589. if(str_price==nil)
  1590. {
  1591. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1592. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1593. if(price==nil)
  1594. Price=@"No Price.";
  1595. else
  1596. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1597. }
  1598. else
  1599. {
  1600. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1601. }
  1602. double discount = sqlite3_column_double(statement, 2);
  1603. int item_count = sqlite3_column_int(statement, 3);
  1604. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1605. NSString *nsline_note=nil;
  1606. if(line_note!=nil)
  1607. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1608. char *name = (char*)sqlite3_column_text(statement, 5);
  1609. NSString *nsname=nil;
  1610. if(name!=nil)
  1611. nsname= [[NSString alloc]initWithUTF8String:name];
  1612. char *description = (char*)sqlite3_column_text(statement, 6);
  1613. NSString *nsdescription=nil;
  1614. if(description!=nil)
  1615. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1616. // int stockUom = sqlite3_column_int(statement, 8);
  1617. // int _id = sqlite3_column_int(statement, 9);
  1618. //
  1619. //
  1620. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1621. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1622. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1623. // int carton=[bsubtotaljson[@"carton"] intValue];
  1624. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1625. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1626. itemjson[@"model"]=nsname;
  1627. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1628. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1629. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1630. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1631. // itemjson[@"check"]=@"true";
  1632. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1633. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1634. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1635. itemjson[@"unit_price"]=Price;
  1636. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1637. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1638. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1639. itemjson[@"note"]=nsline_note;
  1640. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1641. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1642. count++;
  1643. }
  1644. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1645. sqlite3_finalize(statement);
  1646. }
  1647. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1648. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1649. return ret;
  1650. }
  1651. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1652. {
  1653. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1654. sqlite3 *db = [iSalesDB get_db];
  1655. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1656. // for(int i=0;i<arr_soid.count;i++)
  1657. sqlite3_stmt * statement;
  1658. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1659. //int count=0;
  1660. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1661. if ( dbresult== SQLITE_OK)
  1662. {
  1663. int count=0;
  1664. while (sqlite3_step(statement) == SQLITE_ROW)
  1665. {
  1666. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1667. int _id = sqlite3_column_int(statement, 0);
  1668. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1669. NSString* nssync_data=nil;
  1670. if(sync_data!=nil)
  1671. {
  1672. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1673. }
  1674. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1675. NSString* nsimg_1=nil;
  1676. if(img_1!=nil)
  1677. {
  1678. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1679. [self copy_upImg:serial file:nsimg_1];
  1680. }
  1681. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1682. NSString* nsso_no=nil;
  1683. if(so_no!=nil)
  1684. {
  1685. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1686. }
  1687. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1688. itemjson[@"order_type"]=@"submit order";
  1689. else
  1690. itemjson[@"order_type"]=@"archive order";
  1691. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1692. itemjson[@"json_data"]= nssync_data;
  1693. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1694. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1695. count++;
  1696. }
  1697. ret[@"count"]=[NSNumber numberWithInt:count ];
  1698. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1699. }
  1700. sqlite3_finalize(statement);
  1701. [iSalesDB close_db:db];
  1702. return ret;
  1703. }
  1704. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1705. {
  1706. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1707. sqlite3 *db = [iSalesDB get_db];
  1708. // UIApplication * app = [UIApplication sharedApplication];
  1709. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1710. 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";
  1711. sqlite3_stmt * statement;
  1712. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1713. int count=0;
  1714. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1715. if ( dbresult== SQLITE_OK)
  1716. {
  1717. while (sqlite3_step(statement) == SQLITE_ROW)
  1718. {
  1719. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1720. int _id = sqlite3_column_int(statement, 0);
  1721. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1722. NSString* nsimg_0=nil;
  1723. if(img_0!=nil)
  1724. {
  1725. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1726. [self copy_upImg:serial file:nsimg_0];
  1727. }
  1728. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1729. NSString* nsimg_1=nil;
  1730. if(img_1!=nil)
  1731. {
  1732. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1733. [self copy_upImg:serial file:nsimg_1];
  1734. }
  1735. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1736. NSString* nsimg_2=nil;
  1737. if(img_2!=nil)
  1738. {
  1739. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1740. [self copy_upImg:serial file:nsimg_2];
  1741. }
  1742. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1743. NSString* nssync_data=nil;
  1744. if(sync_data!=nil)
  1745. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1746. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1747. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1748. itemjson[@"json_data"]= nssync_data;
  1749. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1750. count++;
  1751. }
  1752. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1753. sqlite3_finalize(statement);
  1754. }
  1755. ret[@"count"]=[NSNumber numberWithInt:count ];
  1756. [iSalesDB close_db:db];
  1757. return ret;
  1758. }
  1759. +(bool) check_offlinedata
  1760. {
  1761. UIApplication * app = [UIApplication sharedApplication];
  1762. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1763. sqlite3 *db = [iSalesDB get_db];
  1764. NSString * where=@"1=1";
  1765. // if(appDelegate.user!=nil)
  1766. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1767. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1768. [iSalesDB close_db:db];
  1769. if(count==0)
  1770. {
  1771. return false;
  1772. }
  1773. return true;
  1774. //
  1775. //[iSalesDB close_db:db];
  1776. }
  1777. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1778. {
  1779. UIApplication * app = [UIApplication sharedApplication];
  1780. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1781. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1782. sqlite3 *db = [iSalesDB get_db];
  1783. NSString* collectId=params[@"collectId"];
  1784. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1785. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1786. [iSalesDB execSql:sqlQuery db:db];
  1787. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1788. [iSalesDB close_db:db];
  1789. appDelegate.wish_count =count;
  1790. [appDelegate update_count_mark];
  1791. ret[@"result"]= [NSNumber numberWithInt:2];
  1792. return ret;
  1793. }
  1794. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1795. //{
  1796. //
  1797. // UIApplication * app = [UIApplication sharedApplication];
  1798. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1799. //
  1800. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1801. // sqlite3 *db = [iSalesDB get_db];
  1802. // NSString* product_id=params[@"product_id"];
  1803. //
  1804. //
  1805. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1806. //
  1807. //
  1808. // for(int i=0;i<arr.count;i++)
  1809. // {
  1810. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1811. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1812. // if(count==0)
  1813. // {
  1814. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1815. // [iSalesDB execSql:sqlQuery db:db];
  1816. // }
  1817. // }
  1818. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1819. // [iSalesDB close_db:db];
  1820. //
  1821. // appDelegate.wish_count =count;
  1822. //
  1823. // [appDelegate update_count_mark];
  1824. // ret[@"result"]= [NSNumber numberWithInt:2];
  1825. // return ret;
  1826. // //
  1827. // //return ret;
  1828. //}
  1829. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1830. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1831. DebugLog(@"time interval: %lf",interval);
  1832. }
  1833. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1834. {
  1835. UIApplication * app = [UIApplication sharedApplication];
  1836. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1837. int sort = [[params objectForKey:@"sort"] intValue];
  1838. NSString *sort_str = @"";
  1839. switch (sort) {
  1840. case 0:{
  1841. sort_str = @"order by w.modify_time desc";
  1842. }
  1843. break;
  1844. case 1:{
  1845. sort_str = @"order by w.modify_time asc";
  1846. }
  1847. break;
  1848. case 2:{
  1849. sort_str = @"order by m.name asc";
  1850. }
  1851. break;
  1852. case 3:{
  1853. sort_str = @"order by m.name desc";
  1854. }
  1855. break;
  1856. case 4:{
  1857. sort_str = @"order by m.description asc";
  1858. }
  1859. break;
  1860. default:
  1861. break;
  1862. }
  1863. NSString* user = appDelegate.user;
  1864. sqlite3 *db = [iSalesDB get_db];
  1865. // order by w.create_time
  1866. 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];
  1867. // 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];
  1868. sqlite3_stmt * statement;
  1869. NSDate *date1 = [NSDate date];
  1870. NSDate *date2 = nil;
  1871. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1872. int count=0;
  1873. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1874. {
  1875. while (sqlite3_step(statement) == SQLITE_ROW)
  1876. {
  1877. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1878. int product_id = sqlite3_column_double(statement, 0);
  1879. char *description = (char*)sqlite3_column_text(statement, 1);
  1880. if(description==nil)
  1881. description= "";
  1882. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1883. int item_id = sqlite3_column_double(statement, 2);
  1884. NSDate *date_image = [NSDate date];
  1885. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1886. printf("image : ");
  1887. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1888. // char *url = (char*)sqlite3_column_text(statement, 3);
  1889. // if(url==nil)
  1890. // url="";
  1891. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1892. int qty = sqlite3_column_int(statement, 3);
  1893. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1894. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1895. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1896. item[@"description"]= nsdescription;
  1897. item[@"img"]= nsurl;
  1898. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1899. count++;
  1900. }
  1901. printf("total time:");
  1902. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1903. ret[@"count"]= [NSNumber numberWithInt:count];
  1904. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1905. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1906. ret[@"result"]= [NSNumber numberWithInt:2];
  1907. appDelegate.wish_count =count;
  1908. [appDelegate update_count_mark];
  1909. sqlite3_finalize(statement);
  1910. }
  1911. [iSalesDB close_db:db];
  1912. return ret;
  1913. }
  1914. +(NSDictionary*) offline_notimpl
  1915. {
  1916. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1917. ret[@"result"]=@"8";
  1918. ret[@"err_msg"]=@"offline mode does not support this function.";
  1919. return ret;
  1920. }
  1921. +(NSDictionary*) offline_home
  1922. {
  1923. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1924. NSString *cachefolder = [paths objectAtIndex:0];
  1925. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1926. NSData* json =nil;
  1927. json=[NSData dataWithContentsOfFile:img_cache];
  1928. if(json==nil)
  1929. return nil;
  1930. NSError *error=nil;
  1931. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1932. return menu;
  1933. }
  1934. +(NSDictionary*) offline_category_menu
  1935. {
  1936. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1937. NSString *cachefolder = [paths objectAtIndex:0];
  1938. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1939. NSData* json =nil;
  1940. json=[NSData dataWithContentsOfFile:img_cache];
  1941. if(json==nil)
  1942. return nil;
  1943. NSError *error=nil;
  1944. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1945. return menu;
  1946. }
  1947. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1948. {
  1949. NSString* offline_command=params[@"offline_Command"];
  1950. NSDictionary* ret=nil;
  1951. if([offline_command isEqualToString:@"model_NIYMAL"])
  1952. {
  1953. NSString* category = params[@"category"];
  1954. ret = [self refresh_model_NIYMAL:category];
  1955. }
  1956. return ret;
  1957. }
  1958. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1959. {
  1960. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1961. [ret setValue:@"2" forKey:@"result"];
  1962. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1963. sqlite3* db= [iSalesDB get_db];
  1964. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1965. [iSalesDB close_db:db];
  1966. [ret setObject:detail1_section forKey:@"detail_1"];
  1967. return ret;
  1968. }
  1969. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1970. {
  1971. UIApplication * app = [UIApplication sharedApplication];
  1972. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1973. // NSArray* arr1 = [self get_user_all_price_type];
  1974. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1975. // NSSet *set1 = [NSSet setWithArray:arr1];
  1976. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1977. // if(appDelegate.contact_id==nil)
  1978. // set2=[set1 mutableCopy];
  1979. // else
  1980. // [set2 intersectsSet:set1];
  1981. // NSArray *retarr = [set2 allObjects];
  1982. NSString* whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1983. // sqlite3 *db = [iSalesDB get_db];
  1984. NSString* sqlQuery = nil;
  1985. if(appDelegate.contact_id==nil)
  1986. {
  1987. if(!appDelegate.bLogin)
  1988. return nil;
  1989. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type 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];
  1990. }
  1991. else
  1992. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type 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];
  1993. sqlite3_stmt * statement;
  1994. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1995. int count=0;
  1996. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1997. {
  1998. while (sqlite3_step(statement) == SQLITE_ROW)
  1999. {
  2000. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  2001. char *name = (char*)sqlite3_column_text(statement, 0);
  2002. if(name==nil)
  2003. name="";
  2004. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2005. // double price = sqlite3_column_double(statement, 1);
  2006. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  2007. // if(isnull==nil)
  2008. // item[nsname]= @"No Price";
  2009. // else
  2010. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2011. char *price = (char*)sqlite3_column_text(statement, 1);
  2012. if(price!=nil)
  2013. {
  2014. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2015. nsprice=[AESCrypt fastdecrypt:nsprice];
  2016. if(nsprice.length>0)
  2017. {
  2018. double dp= [nsprice doubleValue];
  2019. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2020. }
  2021. }
  2022. else
  2023. {
  2024. item[nsname]= @"No Price";
  2025. }
  2026. // int type= sqlite3_column_int(statement, 2);
  2027. //item[@"type"]=@"price";
  2028. // item[nsname]= nsprice;
  2029. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2030. count++;
  2031. }
  2032. ret[@"count"]= [NSNumber numberWithInt:count];
  2033. sqlite3_finalize(statement);
  2034. }
  2035. // [iSalesDB close_db:db];
  2036. return ret;
  2037. }
  2038. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2039. //{
  2040. // NSArray* arr1 = [self get_user_all_price_type:db];
  2041. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2042. //
  2043. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2044. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2045. // // [set2 intersectsSet:set1];
  2046. // //
  2047. // //
  2048. // // NSArray *retarr = [set2 allObjects];
  2049. //
  2050. // NSString* whereprice=nil;
  2051. // if(contact_id==nil)
  2052. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2053. // else
  2054. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2055. //
  2056. //
  2057. // // sqlite3 *db = [iSalesDB get_db];
  2058. //
  2059. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2060. // sqlite3_stmt * statement;
  2061. //
  2062. //
  2063. // NSNumber* ret = nil;
  2064. // double dprice=DBL_MAX;
  2065. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2066. // {
  2067. //
  2068. //
  2069. // while (sqlite3_step(statement) == SQLITE_ROW)
  2070. // {
  2071. //
  2072. // // double val = sqlite3_column_double(statement, 0);
  2073. // char *price = (char*)sqlite3_column_text(statement, 0);
  2074. // if(price!=nil)
  2075. // {
  2076. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2077. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2078. // if(nsprice.length>0)
  2079. // {
  2080. // double dp= [nsprice doubleValue];
  2081. // if(dp<dprice)
  2082. // dprice=dp;
  2083. // }
  2084. // }
  2085. // }
  2086. //
  2087. //
  2088. //
  2089. //
  2090. // sqlite3_finalize(statement);
  2091. //
  2092. //
  2093. //
  2094. //
  2095. // }
  2096. //
  2097. // // [iSalesDB close_db:db];
  2098. //
  2099. // if(dprice==DBL_MAX)
  2100. // ret= nil;
  2101. // else
  2102. // ret= [NSNumber numberWithDouble:dprice];
  2103. // return ret;
  2104. //}
  2105. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2106. {
  2107. NSArray* arr1 = [self get_user_all_price_type:db];
  2108. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2109. NSString* whereprice=nil;
  2110. if(contact_id==nil)
  2111. whereprice=[RAConvertor arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2112. else
  2113. whereprice=[RAConvertor arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2114. // sqlite3 *db = [iSalesDB get_db];
  2115. NSString *productIdCondition = @"1 = 1";
  2116. if (product_id) {
  2117. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2118. }
  2119. NSString *itemIdCondition = @"";
  2120. if (item_id) {
  2121. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2122. }
  2123. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2124. sqlite3_stmt * statement;
  2125. NSNumber* ret = nil;
  2126. double dprice=DBL_MAX;
  2127. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2128. {
  2129. while (sqlite3_step(statement) == SQLITE_ROW)
  2130. {
  2131. // double val = sqlite3_column_double(statement, 0);
  2132. char *price = (char*)sqlite3_column_text(statement, 0);
  2133. if(price!=nil)
  2134. {
  2135. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2136. nsprice=[AESCrypt fastdecrypt:nsprice];
  2137. if(nsprice.length>0)
  2138. {
  2139. double dp= [nsprice doubleValue];
  2140. if(dp<dprice)
  2141. dprice=dp;
  2142. }
  2143. }
  2144. }
  2145. sqlite3_finalize(statement);
  2146. }
  2147. // [iSalesDB close_db:db];
  2148. if(dprice==DBL_MAX)
  2149. ret= nil;
  2150. else
  2151. ret= [NSNumber numberWithDouble:dprice];
  2152. return ret;
  2153. }
  2154. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2155. {
  2156. NSArray* ret=nil;
  2157. // sqlite3 *db = [iSalesDB get_db];
  2158. // no customer assigned , use login user contact_id
  2159. UIApplication * app = [UIApplication sharedApplication];
  2160. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2161. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2162. sqlite3_stmt * statement;
  2163. // int count=0;
  2164. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2165. {
  2166. if (sqlite3_step(statement) == SQLITE_ROW)
  2167. {
  2168. char *val = (char*)sqlite3_column_text(statement, 0);
  2169. if(val==nil)
  2170. val="";
  2171. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2172. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2173. }
  2174. sqlite3_finalize(statement);
  2175. }
  2176. // [iSalesDB close_db:db];
  2177. return ret;
  2178. }
  2179. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2180. {
  2181. // sqlite3 *db = [iSalesDB get_db];
  2182. if(contact_id==nil)
  2183. {
  2184. // no customer assigned , use login user contact_id
  2185. UIApplication * app = [UIApplication sharedApplication];
  2186. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2187. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2188. sqlite3_stmt * statement;
  2189. // int count=0;
  2190. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2191. {
  2192. if (sqlite3_step(statement) == SQLITE_ROW)
  2193. {
  2194. char *val = (char*)sqlite3_column_text(statement, 0);
  2195. if(val==nil)
  2196. val="";
  2197. contact_id = [[NSString alloc]initWithUTF8String:val];
  2198. }
  2199. sqlite3_finalize(statement);
  2200. }
  2201. if(contact_id.length<=0)
  2202. {
  2203. // [iSalesDB close_db:db];
  2204. return nil;
  2205. }
  2206. }
  2207. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2208. sqlite3_stmt * statement;
  2209. NSArray* ret=nil;
  2210. // int count=0;
  2211. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2212. {
  2213. if (sqlite3_step(statement) == SQLITE_ROW)
  2214. {
  2215. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2216. if(price_type==nil)
  2217. price_type="";
  2218. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2219. if(nsprice_type.length>0)
  2220. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2221. else
  2222. ret=nil;
  2223. }
  2224. sqlite3_finalize(statement);
  2225. }
  2226. // [iSalesDB close_db:db];
  2227. return ret;
  2228. }
  2229. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2230. {
  2231. NSString* ret= nil;
  2232. // sqlite3 *db = [iSalesDB get_db];
  2233. NSString *sqlQuery = nil;
  2234. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2235. // 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;
  2236. // 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
  2237. if(product_id==nil && model_name)
  2238. 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;
  2239. else if (product_id)
  2240. 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
  2241. sqlite3_stmt * statement;
  2242. // int count=0;
  2243. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2244. {
  2245. if (sqlite3_step(statement) == SQLITE_ROW)
  2246. {
  2247. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2248. if(imgurl==nil)
  2249. imgurl="";
  2250. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2251. ret=nsimgurl;
  2252. }
  2253. sqlite3_finalize(statement);
  2254. }
  2255. else
  2256. {
  2257. [ret setValue:@"8" forKey:@"result"];
  2258. }
  2259. // [iSalesDB close_db:db];
  2260. DebugLog(@"data string: %@",ret );
  2261. return ret;
  2262. }
  2263. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2264. {
  2265. NSString* ret= nil;
  2266. sqlite3 *db = [iSalesDB get_db];
  2267. NSString *sqlQuery = nil;
  2268. if(product_id==nil)
  2269. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2270. else
  2271. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2272. sqlite3_stmt * statement;
  2273. // int count=0;
  2274. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2275. {
  2276. if (sqlite3_step(statement) == SQLITE_ROW)
  2277. {
  2278. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2279. if(default_category==nil)
  2280. default_category="";
  2281. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2282. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2283. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2284. ret=nsdefault_category;
  2285. }
  2286. sqlite3_finalize(statement);
  2287. }
  2288. else
  2289. {
  2290. [ret setValue:@"8" forKey:@"result"];
  2291. }
  2292. [iSalesDB close_db:db];
  2293. DebugLog(@"data string: %@",ret );
  2294. return ret;
  2295. }
  2296. + (NSString *) categoryIdOfProduct:(NSString *)product_id name:(NSString *)product_name {
  2297. NSString *sql = nil;
  2298. if (product_id != nil) {
  2299. sql = [NSString stringWithFormat:@"select category from product where product_id = '%@';",product_id];
  2300. } else {
  2301. sql = [NSString stringWithFormat:@"select category from product where name = '%@';",product_name];
  2302. }
  2303. NSString *result_set = [iSalesDB jk_queryText:sql];
  2304. result_set = [result_set stringByReplacingOccurrencesOfString:@"%" withString:@""];
  2305. result_set = [result_set stringByReplacingOccurrencesOfString:@"#" withString:@""];
  2306. return result_set;
  2307. }
  2308. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2309. {
  2310. NSString* model_name = [params valueForKey:@"product_name"];
  2311. NSString* product_id = [params valueForKey:@"product_id"];
  2312. NSString* category = [params valueForKey:@"category"];
  2313. NSString *default_category_id = [self model_default_category:product_id model_name:model_name];
  2314. if(category==nil) {
  2315. category = default_category_id;
  2316. } else {
  2317. NSArray *arr_0 = [category componentsSeparatedByString:@","];
  2318. // 参数重有多个category id
  2319. if (arr_0.count > 1) {
  2320. NSArray *arr_1 = [[self categoryIdOfProduct:product_id name:model_name] componentsSeparatedByString:@","];
  2321. // 取交集
  2322. NSMutableSet *set_0 = [NSMutableSet setWithArray:arr_0];
  2323. NSMutableSet *set_1 = [NSMutableSet setWithArray:arr_1];
  2324. [set_0 intersectSet:set_1];
  2325. if (set_0.count == 1) {
  2326. category = (NSString *)[set_0 anyObject];
  2327. } else {
  2328. if ([set_0 containsObject:default_category_id]) {
  2329. category = default_category_id;
  2330. } else {
  2331. category = (NSString *)[set_0 anyObject];
  2332. }
  2333. }
  2334. }
  2335. }
  2336. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2337. sqlite3 *db = [iSalesDB get_db];
  2338. // int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2339. NSString *sqlQuery = nil;
  2340. if(product_id==nil)
  2341. 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,seat_dimension,product_content_writing 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='%@';
  2342. else
  2343. 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,seat_dimension,product_content_writing 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=%@;
  2344. sqlite3_stmt * statement;
  2345. [ret setValue:@"2" forKey:@"result"];
  2346. [ret setValue:@"3" forKey:@"detail_section_count"];
  2347. // int count=0;
  2348. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2349. {
  2350. if (sqlite3_step(statement) == SQLITE_ROW)
  2351. {
  2352. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2353. char *name = (char*)sqlite3_column_text(statement, 0);
  2354. if(name==nil)
  2355. name="";
  2356. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2357. char *description = (char*)sqlite3_column_text(statement, 1);
  2358. if(description==nil)
  2359. description="";
  2360. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2361. int product_id = sqlite3_column_int(statement, 2);
  2362. char *color = (char*)sqlite3_column_text(statement, 3);
  2363. if(color==nil)
  2364. color="";
  2365. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2366. //
  2367. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2368. // if(legcolor==nil)
  2369. // legcolor="";
  2370. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2371. //
  2372. //
  2373. int availability = sqlite3_column_int(statement, 5);
  2374. //
  2375. int incoming_stock = sqlite3_column_int(statement, 6);
  2376. char *demension = (char*)sqlite3_column_text(statement, 7);
  2377. if(demension==nil)
  2378. demension="";
  2379. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2380. // ,,,,,,,,,
  2381. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2382. if(seat_height==nil)
  2383. seat_height="";
  2384. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2385. char *material = (char*)sqlite3_column_text(statement, 9);
  2386. if(material==nil)
  2387. material="";
  2388. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2389. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2390. if(box_dim==nil)
  2391. box_dim="";
  2392. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2393. char *volume = (char*)sqlite3_column_text(statement, 11);
  2394. if(volume==nil)
  2395. volume="";
  2396. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2397. double weight = sqlite3_column_double(statement, 12);
  2398. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2399. if(model_set==nil)
  2400. model_set="";
  2401. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2402. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2403. if(load_ability==nil)
  2404. load_ability="";
  2405. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2406. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2407. if(default_category==nil)
  2408. default_category="";
  2409. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2410. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2411. if(fabric_content==nil)
  2412. fabric_content="";
  2413. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2414. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2415. if(assembling==nil)
  2416. assembling="";
  2417. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2418. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2419. if(made_in==nil)
  2420. made_in="";
  2421. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2422. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2423. if(special_remarks==nil)
  2424. special_remarks="";
  2425. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2426. int stockUcom = sqlite3_column_double(statement, 20);
  2427. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2428. if(product_group==nil)
  2429. product_group="";
  2430. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2431. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2432. // if(fashion_selector==nil)
  2433. // fashion_selector="";
  2434. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2435. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2436. if(selector_field==nil)
  2437. selector_field="";
  2438. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2439. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2440. if(property_field==nil)
  2441. property_field="";
  2442. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2443. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2444. if(packaging==nil)
  2445. packaging="";
  2446. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2447. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2448. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2449. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2450. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2451. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2452. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2453. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2454. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2455. NSString* Availability=nil;
  2456. if(availability>0)
  2457. Availability=[NSString stringWithFormat:@"%d",availability];
  2458. else
  2459. Availability = @"Out of Stock";
  2460. [img_section setValue:Availability forKey:@"Availability"];
  2461. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2462. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2463. char *eta = (char*)sqlite3_column_text(statement, 25);
  2464. if(eta==nil)
  2465. eta="";
  2466. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2467. if ([nseta isEqualToString:@"null"]) {
  2468. nseta = @"";
  2469. }
  2470. if (availability <= 0) {
  2471. [img_section setValue:nseta forKey:@"ETA"];
  2472. }
  2473. int item_id = sqlite3_column_int(statement, 26);
  2474. NSString *seat_dimension = [self textAtColumn:27 statement:statement];
  2475. NSString *content_writing = [self textAtColumn:28 statement:statement];
  2476. NSString* Price=nil;
  2477. if(appDelegate.bLogin==false)
  2478. Price=@"Must Sign in.";
  2479. else
  2480. {
  2481. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2482. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2483. if(price==nil)
  2484. Price=@"No Price.";
  2485. else
  2486. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2487. }
  2488. [img_section setObject:content_writing forKey:@"product_content_writing"];
  2489. [img_section setValue:Price forKey:@"price"];
  2490. [img_section setValue:nsname forKey:@"model_name"];
  2491. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2492. if (appDelegate.order_code) { // 离线order code即so#
  2493. NSString *cartQtySql = [NSString stringWithFormat:@"select c.item_count from offline_cart as c left join offline_order as o on c.so_no = o.so_id where o.so_id = '%@' and c.item_id = %d;",appDelegate.order_code,item_id];
  2494. __block int cartQTY = 0;
  2495. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2496. cartQTY = sqlite3_column_int(stmt, 0);
  2497. }];
  2498. if (cartQTY > 0) {
  2499. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2500. }
  2501. }
  2502. [ret setObject:img_section forKey:@"img_section"];
  2503. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2504. int detail0_item_count=0;
  2505. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2506. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2507. [detail0_section setObject:[self model_kvItem:@"Seat Dimension" value:seat_dimension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2508. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2509. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2510. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2511. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2512. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2513. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2514. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2515. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2516. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2517. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2518. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2519. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2520. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2521. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2522. {
  2523. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2524. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2525. }
  2526. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2527. [detail0_section setValue:@"kv" forKey:@"type"];
  2528. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2529. [ret setObject:detail0_section forKey:@"detail_0"];
  2530. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2531. // [detail1_section setValue:@"detail" forKey:@"target"];
  2532. // [detail1_section setValue:@"popup" forKey:@"action"];
  2533. // [detail1_section setValue:@"content" forKey:@"type"];
  2534. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2535. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2536. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2537. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2538. [ret setObject:detail1_section forKey:@"detail_1"];
  2539. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2540. [detail2_section setValue:@"detail" forKey:@"target"];
  2541. [detail2_section setValue:@"popup" forKey:@"action"];
  2542. [detail2_section setValue:@"content" forKey:@"type"];
  2543. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2544. [detail2_section setValue:@"true" forKey:@"single_row"];
  2545. [detail2_section setValue:@"local" forKey:@"data"];
  2546. [ret setObject:detail2_section forKey:@"detail_2"];
  2547. }
  2548. sqlite3_finalize(statement);
  2549. }
  2550. else
  2551. {
  2552. [ret setValue:@"8" forKey:@"result"];
  2553. }
  2554. // DebugLog(@"count:%d",count);
  2555. [iSalesDB close_db:db];
  2556. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2557. return ret;
  2558. }
  2559. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2560. NSString* orderCode = [params valueForKey:@"orderCode"];
  2561. NSString* keyword = [params valueForKey:@"keyword"];
  2562. keyword=keyword.lowercaseString;
  2563. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  2564. int limit = [[params valueForKey:@"limit"] intValue];
  2565. int offset = [[params valueForKey:@"offset"] intValue];
  2566. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2567. NSString *limit_str = @"";
  2568. if (limited) {
  2569. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2570. }
  2571. sqlite3 *db = [iSalesDB get_db];
  2572. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2573. UIApplication * app = [UIApplication sharedApplication];
  2574. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2575. NSString *sqlQuery = nil;
  2576. if(exactMatch )
  2577. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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 ;
  2578. else
  2579. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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'%%%@%%' or lower(made_in) like '%%%@%%' or lower(material) like '%%%@%%' or lower(packaging) like '%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,keyword,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
  2580. DebugLog(@"offline_search sql:%@",sqlQuery);
  2581. sqlite3_stmt * statement;
  2582. [ret setValue:@"2" forKey:@"result"];
  2583. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2584. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2585. // int count=0;
  2586. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2587. {
  2588. int i=0;
  2589. while (sqlite3_step(statement) == SQLITE_ROW)
  2590. {
  2591. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2592. // char *name = (char*)sqlite3_column_text(statement, 1);
  2593. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2594. char *name = (char*)sqlite3_column_text(statement, 0);
  2595. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2596. char *description = (char*)sqlite3_column_text(statement, 1);
  2597. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2598. int product_id = sqlite3_column_int(statement, 2);
  2599. // char *url = (char*)sqlite3_column_text(statement, 3);
  2600. // if(url==nil)
  2601. // url="";
  2602. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2603. int wid = sqlite3_column_int(statement, 3);
  2604. int closeout = sqlite3_column_int(statement, 4);
  2605. int cid = sqlite3_column_int(statement, 5);
  2606. int wisdelete = sqlite3_column_int(statement, 6);
  2607. int more_color = sqlite3_column_int(statement, 7);
  2608. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2609. if(wid !=0 && wisdelete != 1)
  2610. [item setValue:@"true" forKey:@"wish_exists"];
  2611. else
  2612. [item setValue:@"false" forKey:@"wish_exists"];
  2613. if(closeout==0)
  2614. [item setValue:@"false" forKey:@"is_closeout"];
  2615. else
  2616. [item setValue:@"true" forKey:@"is_closeout"];
  2617. if(cid==0)
  2618. [item setValue:@"false" forKey:@"cart_exists"];
  2619. else
  2620. [item setValue:@"true" forKey:@"cart_exists"];
  2621. if (more_color == 0) {
  2622. [item setObject:@(false) forKey:@"more_color"];
  2623. } else if (more_color == 1) {
  2624. [item setObject:@(true) forKey:@"more_color"];
  2625. }
  2626. [item addEntriesFromDictionary:imgjson];
  2627. // [item setValue:nsurl forKey:@"img"];
  2628. [item setValue:nsname forKey:@"fash_name"];
  2629. [item setValue:nsdescription forKey:@"description"];
  2630. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2631. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2632. i++;
  2633. }
  2634. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2635. [ret setObject:items forKey:@"items"];
  2636. sqlite3_finalize(statement);
  2637. }
  2638. DebugLog(@"count:%d",count);
  2639. [iSalesDB close_db:db];
  2640. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  2641. return ret;
  2642. }
  2643. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2644. {
  2645. return [self search:params limited:YES];
  2646. }
  2647. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2648. {
  2649. UIApplication * app = [UIApplication sharedApplication];
  2650. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2651. appDelegate.disable_trigger=true;
  2652. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2653. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2654. appDelegate.disable_trigger=false;
  2655. //
  2656. // NSString* user = [params valueForKey:@"user"];
  2657. //
  2658. // NSString* password = [params valueForKey:@"password"];
  2659. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2660. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2661. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2662. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2663. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2664. [appDelegate update_count_mark];
  2665. appDelegate.can_show_price =false;
  2666. appDelegate.can_see_price =false;
  2667. appDelegate.can_create_portfolio =false;
  2668. appDelegate.can_create_order =false;
  2669. appDelegate.can_cancel_order =false;
  2670. appDelegate.can_set_cart_price =false;
  2671. appDelegate.can_delete_order =false;
  2672. appDelegate.can_submit_order =false;
  2673. appDelegate.can_set_tearsheet_price =false;
  2674. appDelegate.can_update_contact_info = false;
  2675. appDelegate.save_order_logout = false;
  2676. appDelegate.submit_order_logout = false;
  2677. appDelegate.alert_sold_in_quantities = false;
  2678. appDelegate.ipad_perm =nil ;
  2679. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2680. appDelegate.OrderFilter= nil;
  2681. [appDelegate SetSo:nil];
  2682. [appDelegate set_main_button_panel];
  2683. // sqlite3 *db = [iSalesDB get_db];
  2684. //
  2685. //
  2686. //
  2687. //
  2688. //
  2689. // 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"]];
  2690. //
  2691. //
  2692. //
  2693. //
  2694. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2695. // sqlite3_stmt * statement;
  2696. //
  2697. //
  2698. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2699. //
  2700. //
  2701. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2702. // {
  2703. //
  2704. //
  2705. // if (sqlite3_step(statement) == SQLITE_ROW)
  2706. // {
  2707. //
  2708. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2709. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2710. //
  2711. //
  2712. //
  2713. // int can_show_price = sqlite3_column_int(statement, 0);
  2714. // int can_see_price = sqlite3_column_int(statement, 1);
  2715. //
  2716. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2717. // if(contact_id==nil)
  2718. // contact_id="";
  2719. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2720. //
  2721. // int user_type = sqlite3_column_int(statement, 3);
  2722. //
  2723. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2724. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2725. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2726. // int can_delete_order = sqlite3_column_int(statement, 7);
  2727. // int can_submit_order = sqlite3_column_int(statement, 8);
  2728. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2729. // int can_create_order = sqlite3_column_int(statement, 10);
  2730. //
  2731. //
  2732. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2733. // if(mode==nil)
  2734. // mode="";
  2735. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2736. //
  2737. //
  2738. // char *username = (char*)sqlite3_column_text(statement, 12);
  2739. // if(username==nil)
  2740. // username="";
  2741. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2742. //
  2743. //
  2744. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2745. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2746. // [header setValue:nscontact_id forKey:@"contact_id"];
  2747. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2748. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2749. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2750. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2751. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2752. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2753. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2754. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2755. //
  2756. // [header setValue:nsusername forKey:@"username"];
  2757. //
  2758. //
  2759. // [ret setObject:header forKey:@"header"];
  2760. // [ret setValue:nsmode forKey:@"mode"];
  2761. //
  2762. //
  2763. // }
  2764. //
  2765. //
  2766. //
  2767. // sqlite3_finalize(statement);
  2768. // }
  2769. //
  2770. //
  2771. //
  2772. // [iSalesDB close_db:db];
  2773. //
  2774. //
  2775. //
  2776. //
  2777. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2778. return ret;
  2779. }
  2780. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2781. {
  2782. UIApplication * app = [UIApplication sharedApplication];
  2783. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2784. appDelegate.disable_trigger=true;
  2785. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2786. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2787. appDelegate.disable_trigger=false;
  2788. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2789. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2790. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2791. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2792. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2793. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2794. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2795. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2796. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2797. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2798. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2799. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2800. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2801. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2802. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2803. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2804. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2805. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2806. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2807. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2808. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2809. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2810. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2811. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2812. [arr_name addObject:customer_first_name];
  2813. [arr_name addObject:customer_last_name];
  2814. NSString *contact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  2815. // default ship from
  2816. 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';";
  2817. __block NSString *cid = @"";
  2818. __block NSString *name = @"";
  2819. __block NSString *ext = @"";
  2820. __block NSString *contact = @"";
  2821. __block NSString *email = @"";
  2822. __block NSString *fax = @"";
  2823. __block NSString *phone = @"";
  2824. sqlite3 *db = [iSalesDB get_db];
  2825. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2826. cid = [self textAtColumn:0 statement:statment];
  2827. name = [self textAtColumn:1 statement:statment];
  2828. ext = [self textAtColumn:2 statement:statment];
  2829. contact = [self textAtColumn:3 statement:statment];
  2830. email = [self textAtColumn:4 statement:statment];
  2831. fax = [self textAtColumn:5 statement:statment];
  2832. phone = [self textAtColumn:6 statement:statment];
  2833. }];
  2834. NSString* so_id = [self get_offline_soid:db];
  2835. if(so_id==nil)
  2836. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2837. 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,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",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,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  2838. int result =[iSalesDB execSql:sql_neworder db:db];
  2839. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2840. //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'
  2841. //soId
  2842. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2843. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2844. sqlite3_stmt * statement;
  2845. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2846. {
  2847. if (sqlite3_step(statement) == SQLITE_ROW)
  2848. {
  2849. // char *name = (char*)sqlite3_column_text(statement, 1);
  2850. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2851. //ret = sqlite3_column_int(statement, 0);
  2852. char *soId = (char*)sqlite3_column_text(statement, 0);
  2853. if(soId==nil)
  2854. soId="";
  2855. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2856. [ret setValue:nssoId forKey:@"soId"];
  2857. [ret setValue:nssoId forKey:@"orderCode"];
  2858. }
  2859. sqlite3_finalize(statement);
  2860. }
  2861. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2862. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2863. [iSalesDB close_db:db];
  2864. return [RAConvertor dict2data:ret];
  2865. }
  2866. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2867. {
  2868. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2869. NSString* orderCode = [params valueForKey:@"orderCode"];
  2870. UIApplication * app = [UIApplication sharedApplication];
  2871. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2872. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2873. {
  2874. appDelegate.disable_trigger=true;
  2875. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2876. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2877. appDelegate.disable_trigger=false;
  2878. }
  2879. sqlite3 *db = [iSalesDB get_db];
  2880. int cart_count=[self query_ordercartcount:orderCode db:db];
  2881. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2882. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2883. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2884. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2885. [iSalesDB close_db:db];
  2886. return [RAConvertor dict2data:ret];
  2887. }
  2888. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2889. {
  2890. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2891. sqlite3 *db = [iSalesDB get_db];
  2892. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2893. int count =0;
  2894. if(params[@"count"]!=nil)
  2895. {
  2896. count = [params[@"count"] intValue];
  2897. }
  2898. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2899. NSString* product_id=params[@"product_id"];
  2900. NSString* orderCode=params[@"orderCode"];
  2901. NSString *qty = params[@"qty"];
  2902. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2903. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2904. __block int number_of_outOfStock = 0;
  2905. for(int i=0;i<arr_id.count;i++)
  2906. {
  2907. NSInteger item_qty= count;
  2908. if (qty) {
  2909. item_qty = [qty_arr[i] integerValue];
  2910. }
  2911. if(item_qty==0)
  2912. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2913. // 检查新加Model数量是否大于库存
  2914. if (!appDelegate.can_create_backorder) {
  2915. __block BOOL needContinue = NO;
  2916. [iSalesDB jk_query:[NSString stringWithFormat:@"select availability from product where product_id = %@;",arr_id[i]] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2917. int availability = sqlite3_column_int(stmt, 0);
  2918. // 库存小于购买量为缺货
  2919. if (availability < item_qty || availability <= 0) {
  2920. number_of_outOfStock++;
  2921. needContinue = YES;
  2922. }
  2923. }];
  2924. if (needContinue) {
  2925. continue;
  2926. }
  2927. }
  2928. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2929. NSString* sql=nil;
  2930. sqlite3_stmt *stmt = nil;
  2931. BOOL shouldStep = NO;
  2932. if(_id<0)
  2933. {
  2934. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2935. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2936. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2937. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  2938. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  2939. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2940. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2941. shouldStep = YES;
  2942. }
  2943. else
  2944. {
  2945. if (qty) { // wish list move to cart
  2946. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2947. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2948. sqlite3_bind_int(stmt, 1, _id);
  2949. shouldStep = YES;
  2950. } else {
  2951. sql = [NSString stringWithFormat:@"select c.item_count + %ld,p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %d;",item_qty,_id];
  2952. __block BOOL update = YES;
  2953. if (!appDelegate.can_create_backorder) {
  2954. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2955. int newQTY = sqlite3_column_int(stmt, 0);
  2956. int availability = sqlite3_column_int(stmt, 1);
  2957. if (newQTY > availability) { // 库存不够
  2958. update = NO;
  2959. number_of_outOfStock++;
  2960. }
  2961. }];
  2962. }
  2963. if (update) {
  2964. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2965. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2966. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2967. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2968. shouldStep = YES;
  2969. }
  2970. }
  2971. }
  2972. if (shouldStep) {
  2973. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2974. [iSalesDB execSql:@"ROLLBACK" db:db];
  2975. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2976. [iSalesDB close_db:db];
  2977. DebugLog(@"add to cart error");
  2978. return [RAConvertor dict2data:ret];
  2979. }
  2980. }
  2981. }
  2982. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2983. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2984. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2985. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2986. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2987. if (!appDelegate.can_create_backorder) {
  2988. if (number_of_outOfStock > 0) {
  2989. ret[@"result"]=[NSNumber numberWithInt:8];
  2990. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  2991. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  2992. }
  2993. }
  2994. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2995. [iSalesDB close_db:db];
  2996. return [RAConvertor dict2data:ret];
  2997. }
  2998. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  2999. {
  3000. // UIApplication * app = [UIApplication sharedApplication];
  3001. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3002. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3003. 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];
  3004. sqlite3_stmt * statement;
  3005. int count=0;
  3006. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3007. {
  3008. while (sqlite3_step(statement) == SQLITE_ROW)
  3009. {
  3010. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  3011. int bitem_id = sqlite3_column_int(statement, 0);
  3012. int bitem_qty = sqlite3_column_int(statement, 1);
  3013. char *name = (char*)sqlite3_column_text(statement, 2);
  3014. if(name==nil)
  3015. name="";
  3016. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  3017. char *description = (char*)sqlite3_column_text(statement, 3);
  3018. if(description==nil)
  3019. description="";
  3020. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  3021. double unit_price = sqlite3_column_double(statement, 4);
  3022. int use_unitprice = sqlite3_column_int(statement, 5);
  3023. if(use_unitprice!=1)
  3024. {
  3025. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  3026. }
  3027. itemjson[@"model"]=nsname;
  3028. itemjson[@"description"]=nsdescription;
  3029. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  3030. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  3031. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  3032. if(compute)
  3033. {
  3034. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  3035. }
  3036. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3037. count++;
  3038. }
  3039. sqlite3_finalize(statement);
  3040. }
  3041. ret[@"count"]=@(count);
  3042. if(count==0)
  3043. return nil;
  3044. else
  3045. return ret;
  3046. }
  3047. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3048. {
  3049. //compute: add part to subtotal;
  3050. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3051. dict_item[@(item_id)]=@"1";
  3052. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3053. double cuft=0;
  3054. double weight=0;
  3055. int carton=0;
  3056. 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];
  3057. sqlite3_stmt * statement;
  3058. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3059. {
  3060. if (sqlite3_step(statement) == SQLITE_ROW)
  3061. {
  3062. double ulength = sqlite3_column_double(statement, 0);
  3063. double uwidth = sqlite3_column_double(statement, 1);
  3064. double uheight = sqlite3_column_double(statement, 2);
  3065. double uweight = sqlite3_column_double(statement, 3);
  3066. double mlength = sqlite3_column_double(statement, 4);
  3067. double mwidth = sqlite3_column_double(statement, 5);
  3068. double mheight = sqlite3_column_double(statement, 6);
  3069. double mweight = sqlite3_column_double(statement, 7);
  3070. double ilength = sqlite3_column_double(statement, 8);
  3071. double iwidth = sqlite3_column_double(statement, 9);
  3072. double iheight = sqlite3_column_double(statement, 10);
  3073. double iweight = sqlite3_column_double(statement, 11);
  3074. int pcs = sqlite3_column_int(statement,12);
  3075. int mpack = sqlite3_column_int(statement, 13);
  3076. int ipack = sqlite3_column_int(statement, 14);
  3077. double ucbf = sqlite3_column_double(statement, 15);
  3078. double icbf = sqlite3_column_double(statement, 16);
  3079. double mcbf = sqlite3_column_double(statement, 17);
  3080. if(ipack==0)
  3081. {
  3082. carton= count/mpack ;
  3083. weight = mweight*carton;
  3084. cuft= carton*(mlength*mwidth*mheight);
  3085. int remain=count%mpack;
  3086. if(remain==0)
  3087. {
  3088. //do nothing;
  3089. }
  3090. else
  3091. {
  3092. carton++;
  3093. weight += uweight*remain;
  3094. cuft += (ulength*uwidth*uheight)*remain;
  3095. }
  3096. }
  3097. else
  3098. {
  3099. carton = count/(mpack*ipack);
  3100. weight = mweight*carton;
  3101. cuft= carton*(mlength*mwidth*mheight);
  3102. int remain=count%(mpack*ipack);
  3103. if(remain==0)
  3104. {
  3105. // do nothing;
  3106. }
  3107. else
  3108. {
  3109. carton++;
  3110. int icarton =remain/ipack;
  3111. int iremain=remain%ipack;
  3112. weight += iweight*icarton;
  3113. cuft += (ilength*iwidth*iheight)*icarton;
  3114. if(iremain==0)
  3115. {
  3116. //do nothing;
  3117. }
  3118. else
  3119. {
  3120. weight += uweight*iremain;
  3121. cuft += (ulength*uwidth*uheight)*iremain;
  3122. }
  3123. }
  3124. }
  3125. #ifdef BUILD_NPD
  3126. cuft=ucbf*count;
  3127. weight= uweight*count;
  3128. #endif
  3129. #if defined(BUILD_HOMER) || defined(BUILD_GATIT)
  3130. cuft=ucbf*count;
  3131. weight= uweight*count;
  3132. #endif
  3133. }
  3134. sqlite3_finalize(statement);
  3135. }
  3136. if(compute)
  3137. {
  3138. NSArray * arr_count=nil;
  3139. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3140. for(int i=0;i<arr_bundle.count;i++)
  3141. {
  3142. dict_item[arr_bundle[i]]=@"1";
  3143. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3144. cuft+=[bundlejson[@"cuft"] doubleValue];
  3145. weight+=[bundlejson[@"weight"] doubleValue];
  3146. carton+=[bundlejson[@"carton"] intValue];
  3147. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3148. }
  3149. }
  3150. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3151. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3152. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3153. ret[@"items"]=dict_item;
  3154. return ret;
  3155. }
  3156. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3157. {
  3158. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3159. // 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 ];
  3160. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3161. DebugLog(@"offline_login sql:%@",sqlQuery);
  3162. sqlite3_stmt * statement;
  3163. int cart_count=0;
  3164. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3165. if ( dbresult== SQLITE_OK)
  3166. {
  3167. while (sqlite3_step(statement) == SQLITE_ROW)
  3168. {
  3169. int item_id = sqlite3_column_int(statement, 0);
  3170. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3171. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3172. }
  3173. sqlite3_finalize(statement);
  3174. }
  3175. return cart_count;
  3176. }
  3177. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3178. {
  3179. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3180. sqlite3 *db = [iSalesDB get_db];
  3181. UIApplication * app = [UIApplication sharedApplication];
  3182. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3183. NSString* orderCode=params[@"orderCode"];
  3184. int sort = [[params objectForKey:@"sort"] intValue];
  3185. NSString *sort_str = @"";
  3186. switch (sort) {
  3187. case 0:{
  3188. sort_str = @"order by c.modify_time desc";
  3189. }
  3190. break;
  3191. case 1:{
  3192. sort_str = @"order by c.modify_time asc";
  3193. }
  3194. break;
  3195. case 2:{
  3196. sort_str = @"order by m.name asc";
  3197. }
  3198. break;
  3199. case 3:{
  3200. sort_str = @"order by m.name desc";
  3201. }
  3202. break;
  3203. case 4:{
  3204. sort_str = @"order by m.description asc";
  3205. }
  3206. break;
  3207. default:
  3208. break;
  3209. }
  3210. 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,m.availability 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 ];
  3211. // 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 ];
  3212. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3213. DebugLog(@"offline_login sql:%@",sqlQuery);
  3214. sqlite3_stmt * statement;
  3215. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3216. NSDate *date1 = [NSDate date];
  3217. int count=0;
  3218. int cart_count=0;
  3219. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3220. if ( dbresult== SQLITE_OK)
  3221. {
  3222. while (sqlite3_step(statement) == SQLITE_ROW)
  3223. {
  3224. // NSDate *row_date = [NSDate date];
  3225. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3226. int product_id = sqlite3_column_int(statement, 0);
  3227. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3228. int item_id = sqlite3_column_int(statement, 7);
  3229. NSString* Price=nil;
  3230. if(str_price==nil)
  3231. {
  3232. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3233. // NSDate *price_date = [NSDate date];
  3234. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3235. // DebugLog(@"price time interval");
  3236. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3237. if(price==nil)
  3238. Price=@"No Price.";
  3239. else
  3240. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3241. }
  3242. else
  3243. {
  3244. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3245. }
  3246. double discount = sqlite3_column_double(statement, 2);
  3247. int item_count = sqlite3_column_int(statement, 3);
  3248. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3249. NSString *nsline_note=nil;
  3250. if(line_note!=nil)
  3251. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3252. char *name = (char*)sqlite3_column_text(statement, 5);
  3253. NSString *nsname=nil;
  3254. if(name!=nil)
  3255. nsname= [[NSString alloc]initWithUTF8String:name];
  3256. char *description = (char*)sqlite3_column_text(statement, 6);
  3257. NSString *nsdescription=nil;
  3258. if(description!=nil)
  3259. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3260. int stockUom = sqlite3_column_int(statement, 8);
  3261. int _id = sqlite3_column_int(statement, 9);
  3262. int availability = sqlite3_column_int(statement, 10);
  3263. // NSDate *subtotal_date = [NSDate date];
  3264. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3265. // DebugLog(@"subtotal_date time interval");
  3266. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3267. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3268. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3269. int carton=[bsubtotaljson[@"carton"] intValue];
  3270. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3271. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3272. // NSDate *img_date = [NSDate date];
  3273. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3274. // DebugLog(@"img_date time interval");
  3275. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3276. itemjson[@"model"]=nsname;
  3277. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3278. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3279. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3280. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3281. itemjson[@"check"]=@"true";
  3282. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3283. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3284. itemjson[@"unit_price"]=Price;
  3285. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3286. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3287. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3288. itemjson[@"note"]=nsline_note;
  3289. if (!appDelegate.can_create_backorder) {
  3290. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3291. }
  3292. // NSDate *date2 = [NSDate date];
  3293. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3294. // DebugLog(@"model_bundle time interval");
  3295. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3296. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3297. count++;
  3298. // DebugLog(@"row time interval");
  3299. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3300. }
  3301. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3302. sqlite3_finalize(statement);
  3303. }
  3304. DebugLog(@"request cart total time interval");
  3305. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3306. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3307. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3308. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3309. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3310. ret[@"count"]=[NSNumber numberWithInt:count ];
  3311. ret[@"mode"]=@"Regular Mode";
  3312. [iSalesDB close_db:db];
  3313. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3314. DebugLog(@"general notes :%@",general_note);
  3315. ret[@"general_note"]= general_note;
  3316. return [RAConvertor dict2data:ret];
  3317. }
  3318. +(NSData*) offline_login :(NSMutableDictionary *) params
  3319. {
  3320. NSString* user = [params valueForKey:@"user"];
  3321. NSString* password = [params valueForKey:@"password"];
  3322. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3323. sqlite3 *db = [iSalesDB get_db];
  3324. 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"]];
  3325. DebugLog(@"offline_login sql:%@",sqlQuery);
  3326. sqlite3_stmt * statement;
  3327. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3328. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3329. {
  3330. if (sqlite3_step(statement) == SQLITE_ROW)
  3331. {
  3332. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3333. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3334. int can_show_price = sqlite3_column_int(statement, 0);
  3335. int can_see_price = sqlite3_column_int(statement, 1);
  3336. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3337. if(contact_id==nil)
  3338. contact_id="";
  3339. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3340. int user_type = sqlite3_column_int(statement, 3);
  3341. int can_cancel_order = sqlite3_column_int(statement, 4);
  3342. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3343. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3344. int can_delete_order = sqlite3_column_int(statement, 7);
  3345. int can_submit_order = sqlite3_column_int(statement, 8);
  3346. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3347. int can_create_order = sqlite3_column_int(statement, 10);
  3348. char *mode = (char*)sqlite3_column_text(statement, 11);
  3349. if(mode==nil)
  3350. mode="";
  3351. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3352. char *username = (char*)sqlite3_column_text(statement, 12);
  3353. if(username==nil)
  3354. username="";
  3355. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3356. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3357. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3358. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3359. [header setValue:nscontact_id forKey:@"contact_id"];
  3360. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3361. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3362. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3363. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3364. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3365. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3366. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3367. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3368. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3369. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3370. [header setValue:nsusername forKey:@"username"];
  3371. NSError* error=nil;
  3372. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3373. [header setValue:statusFilter forKey:@"statusFilter"];
  3374. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3375. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3376. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3377. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3378. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3379. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3380. [ret setObject:header forKey:@"header"];
  3381. [ret setValue:nsmode forKey:@"mode"];
  3382. }
  3383. sqlite3_finalize(statement);
  3384. }
  3385. [iSalesDB close_db:db];
  3386. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3387. return [RAConvertor dict2data:ret];
  3388. }
  3389. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3390. {
  3391. NSString* contactId = [params valueForKey:@"contactId"];
  3392. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3393. sqlite3 *db = [iSalesDB get_db];
  3394. NSString *sqlQuery = nil;
  3395. {
  3396. 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,type from offline_contact where contact_id='%@'",contactId];
  3397. }
  3398. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3399. sqlite3_stmt * statement;
  3400. [ret setValue:@"2" forKey:@"result"];
  3401. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3402. {
  3403. //int i = 0;
  3404. if (sqlite3_step(statement) == SQLITE_ROW)
  3405. {
  3406. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3407. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3408. // int editable = sqlite3_column_int(statement, 0);
  3409. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3410. NSString *nscompany_name =nil;
  3411. if(company_name==nil)
  3412. nscompany_name=@"";
  3413. else
  3414. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3415. char *country = (char*)sqlite3_column_text(statement, 2);
  3416. if(country==nil)
  3417. country="";
  3418. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3419. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3420. // if(addr==nil)
  3421. // addr="";
  3422. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3423. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3424. if(zipcode==nil)
  3425. zipcode="";
  3426. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3427. char *state = (char*)sqlite3_column_text(statement, 5);
  3428. if(state==nil)
  3429. state="";
  3430. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3431. char *city = (char*)sqlite3_column_text(statement, 6);
  3432. if(city==nil)
  3433. city="";
  3434. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3435. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3436. // NSString *nscontact_name = nil;
  3437. // if(contact_name==nil)
  3438. // nscontact_name=@"";
  3439. // else
  3440. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3441. char *phone = (char*)sqlite3_column_text(statement, 8);
  3442. NSString *nsphone = nil;
  3443. if(phone==nil)
  3444. nsphone=@"";
  3445. else
  3446. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3447. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3448. if(contact_id==nil)
  3449. contact_id="";
  3450. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3451. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3452. if(addr_1==nil)
  3453. addr_1="";
  3454. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3455. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3456. if(addr_2==nil)
  3457. addr_2="";
  3458. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3459. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3460. if(addr_3==nil)
  3461. addr_3="";
  3462. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3463. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3464. if(addr_4==nil)
  3465. addr_4="";
  3466. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3467. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3468. if(first_name==nil)
  3469. first_name="";
  3470. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3471. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3472. if(last_name==nil)
  3473. last_name="";
  3474. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3475. char *fax = (char*)sqlite3_column_text(statement, 16);
  3476. NSString *nsfax = nil;
  3477. if(fax==nil)
  3478. nsfax=@"";
  3479. else
  3480. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3481. char *email = (char*)sqlite3_column_text(statement, 17);
  3482. NSString *nsemail = nil;
  3483. if(email==nil)
  3484. nsemail=@"";
  3485. else
  3486. nsemail= [[NSString alloc]initWithUTF8String:email];
  3487. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3488. NSString *nsimg_0 = nil;
  3489. if(img_0==nil)
  3490. nsimg_0=@"";
  3491. else
  3492. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3493. [self copy_bcardImg:nsimg_0];
  3494. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3495. NSString *nsimg_1 = nil;
  3496. if(img_1==nil)
  3497. nsimg_1=@"";
  3498. else
  3499. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3500. [self copy_bcardImg:nsimg_1];
  3501. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3502. NSString *nsimg_2 = nil;
  3503. if(img_2==nil)
  3504. nsimg_2=@"";
  3505. else
  3506. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3507. [self copy_bcardImg:nsimg_2];
  3508. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3509. NSString *nsprice_type = nil;
  3510. if(price_type==nil)
  3511. nsprice_type=@"";
  3512. else
  3513. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3514. char *notes = (char*)sqlite3_column_text(statement, 22);
  3515. NSString *nsnotes = nil;
  3516. if(notes==nil)
  3517. nsnotes=@"";
  3518. else
  3519. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3520. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3521. NSString *nssalesrep = nil;
  3522. if(salesrep==nil)
  3523. nssalesrep=@"";
  3524. else
  3525. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3526. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3527. {
  3528. // decrypt
  3529. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3530. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3531. nsphone=[AESCrypt fastdecrypt:nsphone];
  3532. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3533. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3534. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3535. }
  3536. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3537. [arr_name addObject:nsfirst_name];
  3538. [arr_name addObject:nslast_name];
  3539. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  3540. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3541. [arr_ext addObject:nsaddr_1];
  3542. [arr_ext addObject:nsaddr_2];
  3543. [arr_ext addObject:nsaddr_3];
  3544. [arr_ext addObject:nsaddr_4];
  3545. [arr_ext addObject:@"\r\n"];
  3546. [arr_ext addObject:nscity];
  3547. [arr_ext addObject:nsstate];
  3548. [arr_ext addObject:nszipcode];
  3549. [arr_ext addObject:nscountry];
  3550. NSString *nsext=[RAConvertor arr2string:arr_ext separator:@", " trim:true];
  3551. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3552. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3553. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3554. [item setValue:nscountry forKey:@"customer_country"];
  3555. [item setValue:nsphone forKey:@"customer_phone"];
  3556. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3557. [item setValue:nscompany_name forKey:@"customer_name"];
  3558. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3559. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3560. [item setValue:nsext forKey:@"customer_contact_ext"];
  3561. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3562. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3563. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3564. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3565. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3566. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3567. [item setValue:nslast_name forKey:@"customer_last_name"];
  3568. [item setValue:nscity forKey:@"customer_city"];
  3569. [item setValue:nsstate forKey:@"customer_state"];
  3570. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3571. [item setValue:contactId forKey:@"customer_cid"];
  3572. [item setValue:nscontact_name forKey:@"customer_contact"];
  3573. [item setValue:nsfax forKey:@"customer_fax"];
  3574. [item setValue:nsemail forKey:@"customer_email"];
  3575. [item setValue:contact_type forKey:@"customer_contact_type"];
  3576. [ret setObject:item forKey:@"customerInfo"];
  3577. // i++;
  3578. }
  3579. UIApplication * app = [UIApplication sharedApplication];
  3580. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3581. [ret setValue:appDelegate.mode forKey:@"mode"];
  3582. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3583. sqlite3_finalize(statement);
  3584. }
  3585. [iSalesDB close_db:db];
  3586. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  3587. return ret;
  3588. }
  3589. + (bool) copy_bcardImg:(NSString*) filename
  3590. {
  3591. if(filename.length==0)
  3592. return false;
  3593. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3594. bool ret=false;
  3595. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3596. NSString *cachefolder = [paths objectAtIndex:0];
  3597. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3598. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3599. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3600. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3601. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3602. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3603. //
  3604. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3605. NSFileManager* fileManager = [NSFileManager defaultManager];
  3606. BOOL bdir=NO;
  3607. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3608. {
  3609. NSError *error = nil;
  3610. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3611. {
  3612. ret=false;
  3613. }
  3614. else
  3615. {
  3616. ret=true;
  3617. }
  3618. // NSError *error = nil;
  3619. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3620. //
  3621. // if(!bsuccess)
  3622. // {
  3623. // DebugLog(@"Create offline_createimg folder failed");
  3624. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3625. // return [RAConvertor dict2data:ret];
  3626. // }
  3627. // if(bsuccess)
  3628. // {
  3629. // sqlite3 *db = [self get_db];
  3630. //
  3631. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3632. // [iSalesDB close_db:db];
  3633. // }
  3634. }
  3635. return ret;
  3636. //
  3637. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3638. // if(bsuccess)
  3639. // {
  3640. // NSError *error = nil;
  3641. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3642. // {
  3643. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3644. // }
  3645. // else
  3646. // {
  3647. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3648. //
  3649. // ret[@"img_url_aname"]=filename;
  3650. // ret[@"img_url"]=savedImagePath;
  3651. // }
  3652. // }
  3653. }
  3654. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3655. {
  3656. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3657. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3658. NSString *cachefolder = [paths objectAtIndex:0];
  3659. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3660. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3661. NSFileManager* fileManager = [NSFileManager defaultManager];
  3662. BOOL bdir=YES;
  3663. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3664. {
  3665. NSError *error = nil;
  3666. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3667. if(!bsuccess)
  3668. {
  3669. DebugLog(@"Create offline_createimg folder failed");
  3670. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3671. return [RAConvertor dict2data:ret];
  3672. }
  3673. // if(bsuccess)
  3674. // {
  3675. // sqlite3 *db = [self get_db];
  3676. //
  3677. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3678. // [iSalesDB close_db:db];
  3679. // }
  3680. }
  3681. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3682. //JEPG格式
  3683. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3684. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3685. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3686. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3687. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3688. if(bsuccess)
  3689. {
  3690. NSError *error = nil;
  3691. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3692. {
  3693. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3694. }
  3695. else
  3696. {
  3697. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3698. ret[@"img_url_aname"]=filename;
  3699. ret[@"img_url"]=filename;
  3700. }
  3701. }
  3702. else
  3703. {
  3704. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3705. }
  3706. return [RAConvertor dict2data:ret];
  3707. }
  3708. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3709. {
  3710. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3711. category = [category substringToIndex:3];
  3712. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3713. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3714. params[@"category"]= category;
  3715. ret[@"params"]= params;
  3716. [ret setValue:@"detail" forKey:@"target"];
  3717. [ret setValue:@"popup" forKey:@"action"];
  3718. [ret setValue:@"content" forKey:@"type"];
  3719. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3720. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3721. [ret setValue:@"true" forKey:@"single_row"];
  3722. [ret setValue:@"true" forKey:@"partial_refresh"];
  3723. // sqlite3 *db = [iSalesDB get_db];
  3724. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3725. 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 ;
  3726. sqlite3_stmt * statement;
  3727. int count = 0;
  3728. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3729. // int count=0;
  3730. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3731. {
  3732. int i=0;
  3733. while (sqlite3_step(statement) == SQLITE_ROW)
  3734. {
  3735. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3736. // char *name = (char*)sqlite3_column_text(statement, 1);
  3737. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3738. char *name = (char*)sqlite3_column_text(statement, 0);
  3739. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3740. int product_id = sqlite3_column_int(statement, 1);
  3741. char *url = (char*)sqlite3_column_text(statement, 2);
  3742. if(url==nil)
  3743. url="";
  3744. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3745. [item setValue:nsurl forKey:@"picture_path"];
  3746. [item setValue:nsname forKey:@"fash_name"];
  3747. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3748. [item setValue:category forKey:@"category"];
  3749. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3750. i++;
  3751. }
  3752. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3753. sqlite3_finalize(statement);
  3754. }
  3755. DebugLog(@"count:%d",count);
  3756. // [iSalesDB close_db:db];
  3757. return ret;
  3758. }
  3759. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3760. {
  3761. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3762. [ret setValue:key forKey:@"key"];
  3763. [ret setValue:value forKey:@"val"];
  3764. [ret setValue:@"price" forKey:@"type"];
  3765. return ret;
  3766. }
  3767. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3768. {
  3769. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3770. [ret setValue:key forKey:@"key"];
  3771. [ret setValue:value forKey:@"val"];
  3772. return ret;
  3773. }
  3774. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3775. {
  3776. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3777. [ret setValue:@"0" forKey:@"img_count"];
  3778. // sqlite3 *db = [iSalesDB get_db];
  3779. 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 ;
  3780. sqlite3_stmt * statement;
  3781. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3782. {
  3783. int i=0;
  3784. if (sqlite3_step(statement) == SQLITE_ROW)
  3785. {
  3786. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3787. // char *name = (char*)sqlite3_column_text(statement, 1);
  3788. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3789. char *value = (char*)sqlite3_column_text(statement, 0);
  3790. if(value==nil)
  3791. value="";
  3792. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3793. char *key = (char*)sqlite3_column_text(statement, 1);
  3794. if(key==nil)
  3795. key="";
  3796. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3797. [item setValue:nsvalue forKey:@"val"];
  3798. [item setValue:nskey forKey:@"key"];
  3799. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3800. [ret setValue:@"1" forKey:@"count"];
  3801. i++;
  3802. }
  3803. sqlite3_finalize(statement);
  3804. }
  3805. // [iSalesDB close_db:db];
  3806. return ret;
  3807. }
  3808. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3809. {
  3810. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3811. [ret setValue:@"0" forKey:@"count"];
  3812. // sqlite3 *db = [iSalesDB get_db];
  3813. 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;
  3814. sqlite3_stmt * statement;
  3815. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3816. {
  3817. int i=0;
  3818. while (sqlite3_step(statement) == SQLITE_ROW)
  3819. {
  3820. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3821. // char *name = (char*)sqlite3_column_text(statement, 1);
  3822. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3823. char *value = (char*)sqlite3_column_text(statement, 0);
  3824. if(value==nil)
  3825. value="";
  3826. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3827. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3828. if(selector_display==nil)
  3829. selector_display="";
  3830. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3831. int product_id = sqlite3_column_int(statement, 2);
  3832. char *category = (char*)sqlite3_column_text(statement, 3);
  3833. if(category==nil)
  3834. category="";
  3835. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3836. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3837. [item setValue:nsvalue forKey:@"title"];
  3838. [item setValue:url forKey:@"pic_url"];
  3839. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3840. [params setValue:@"2" forKey:@"count"];
  3841. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3842. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3843. [param0 setValue:@"product_id" forKey:@"name"];
  3844. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3845. [param1 setValue:nscategory forKey:@"val"];
  3846. [param1 setValue:@"category" forKey:@"name"];
  3847. [params setObject:param0 forKey:@"param_0"];
  3848. [params setObject:param1 forKey:@"param_1"];
  3849. [item setObject:params forKey:@"params"];
  3850. [ret setValue:nsselector_display forKey:@"name"];
  3851. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3852. i++;
  3853. }
  3854. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3855. [ret setValue:@"switch" forKey:@"action"];
  3856. sqlite3_finalize(statement);
  3857. }
  3858. // [iSalesDB close_db:db];
  3859. return ret;
  3860. }
  3861. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3862. {
  3863. // model 在 category search 显示的图片。
  3864. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3865. // sqlite3 *db = [iSalesDB get_db];
  3866. 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];
  3867. sqlite3_stmt * statement;
  3868. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3869. {
  3870. while (sqlite3_step(statement) == SQLITE_ROW)
  3871. {
  3872. char *url = (char*)sqlite3_column_text(statement, 0);
  3873. if(url==nil)
  3874. url="";
  3875. int type = sqlite3_column_int(statement, 1);
  3876. if(type==0)
  3877. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3878. else
  3879. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3880. }
  3881. sqlite3_finalize(statement);
  3882. }
  3883. // [iSalesDB close_db:db];
  3884. return ret;
  3885. }
  3886. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3887. {
  3888. int item_id=-1;
  3889. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3890. sqlite3_stmt * statement;
  3891. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3892. {
  3893. if (sqlite3_step(statement) == SQLITE_ROW)
  3894. {
  3895. item_id = sqlite3_column_int(statement, 0);
  3896. }
  3897. sqlite3_finalize(statement);
  3898. }
  3899. return item_id;
  3900. }
  3901. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3902. {
  3903. // NSString* ret = @"";
  3904. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3905. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3906. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3907. sqlite3_stmt * statement;
  3908. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3909. {
  3910. while (sqlite3_step(statement) == SQLITE_ROW)
  3911. {
  3912. int bitem_id = sqlite3_column_int(statement, 0);
  3913. int bitem_qty = sqlite3_column_int(statement, 1);
  3914. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3915. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3916. }
  3917. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3918. sqlite3_finalize(statement);
  3919. }
  3920. // if(ret==nil)
  3921. // ret=@"";
  3922. *count=arr_count;
  3923. return arr_bundle;
  3924. }
  3925. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3926. {
  3927. // get default sold qty, return -1 if model not found;
  3928. int ret = -1;
  3929. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3930. sqlite3_stmt * statement;
  3931. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3932. {
  3933. if (sqlite3_step(statement) == SQLITE_ROW)
  3934. {
  3935. ret = sqlite3_column_int(statement, 0);
  3936. }
  3937. sqlite3_finalize(statement);
  3938. }
  3939. return ret;
  3940. }
  3941. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3942. {
  3943. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3944. [ret setValue:@"0" forKey:@"img_count"];
  3945. // sqlite3 *db = [iSalesDB get_db];
  3946. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3947. sqlite3_stmt * statement;
  3948. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3949. {
  3950. int i=0;
  3951. while (sqlite3_step(statement) == SQLITE_ROW)
  3952. {
  3953. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3954. // char *name = (char*)sqlite3_column_text(statement, 1);
  3955. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3956. char *url = (char*)sqlite3_column_text(statement, 0);
  3957. if(url==nil)
  3958. url="";
  3959. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3960. [item setValue:nsurl forKey:@"s"];
  3961. [item setValue:nsurl forKey:@"l"];
  3962. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3963. i++;
  3964. }
  3965. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3966. sqlite3_finalize(statement);
  3967. }
  3968. // [iSalesDB close_db:db];
  3969. return ret;
  3970. }
  3971. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3972. UIApplication * app = [UIApplication sharedApplication];
  3973. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3974. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3975. sqlite3 *db = [iSalesDB get_db];
  3976. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3977. NSString* product_id=params[@"product_id"];
  3978. NSString *item_count_str = params[@"item_count"];
  3979. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3980. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3981. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3982. // NSString *sql = @"";
  3983. for(int i=0;i<arr.count;i++)
  3984. {
  3985. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3986. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3987. __block int cart_count = 0;
  3988. if (!item_count_str) {
  3989. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3990. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3991. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3992. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3993. cart_count = [[model_set_components lastObject] integerValue];
  3994. }];
  3995. }
  3996. if(count==0)
  3997. {
  3998. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3999. sqlite3_stmt *stmt;
  4000. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  4001. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  4002. if (item_count_arr) {
  4003. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  4004. } else {
  4005. sqlite3_bind_int(stmt,2,cart_count);
  4006. }
  4007. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  4008. [iSalesDB execSql:@"ROLLBACK" db:db];
  4009. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  4010. [iSalesDB close_db:db];
  4011. DebugLog(@"add to wishlist error");
  4012. return ret;
  4013. }
  4014. } else {
  4015. int qty = 0;
  4016. if (item_count_arr) {
  4017. qty = [item_count_arr[i] integerValue];
  4018. } else {
  4019. qty = cart_count;
  4020. }
  4021. 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]];
  4022. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  4023. [iSalesDB execSql:@"ROLLBACK" db:db];
  4024. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  4025. [iSalesDB close_db:db];
  4026. DebugLog(@"add to wishlist error");
  4027. return ret;
  4028. }
  4029. }
  4030. }
  4031. // [iSalesDB execSql:sql db:db];
  4032. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  4033. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4034. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  4035. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  4036. [iSalesDB close_db:db];
  4037. appDelegate.wish_count =count;
  4038. [appDelegate update_count_mark];
  4039. ret[@"result"]= [NSNumber numberWithInt:2];
  4040. return ret;
  4041. }
  4042. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  4043. // 0 category
  4044. // 1 search
  4045. // 2 itemsearch
  4046. NSData *ret = nil;
  4047. NSDictionary *items = nil;
  4048. switch (from) {
  4049. case 0:{
  4050. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4051. }
  4052. break;
  4053. case 1:{
  4054. items = [[self search:params limited:NO] objectForKey:@"items"];
  4055. }
  4056. break;
  4057. case 2:{
  4058. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4059. }
  4060. break;
  4061. default:
  4062. break;
  4063. }
  4064. if (!items) {
  4065. return ret;
  4066. }
  4067. int count = [[items objectForKey:@"count"] intValue];
  4068. NSMutableString *product_id_str = [@"" mutableCopy];
  4069. for (int i = 0; i < count; i++) {
  4070. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4071. NSDictionary *item = [items objectForKey:key];
  4072. NSString *product_id = [item objectForKey:@"product_id"];
  4073. if (i == 0) {
  4074. [product_id_str appendString:product_id];
  4075. } else {
  4076. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4077. }
  4078. }
  4079. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4080. if ([add_to isEqualToString:@"cart"]) {
  4081. NSString *order_code = [params objectForKey:@"orderCode"];
  4082. if (order_code.length) {
  4083. NSDictionary *newParams = @{
  4084. @"product_id" : product_id_str,
  4085. @"orderCode" : order_code
  4086. };
  4087. ret = [self offline_add2cart:newParams.mutableCopy];
  4088. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4089. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4090. ret = [RAConvertor dict2data:retDic];
  4091. }
  4092. } else if([add_to isEqualToString:@"wishlist"]) {
  4093. NSDictionary *newParams = @{
  4094. @"product_id" : product_id_str
  4095. };
  4096. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4097. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4098. ret = [RAConvertor dict2data:retDic];
  4099. } else if([add_to isEqualToString:@"portfolio"]) {
  4100. NSDictionary *newParams = @{
  4101. @"product_id" : product_id_str
  4102. };
  4103. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4104. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4105. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4106. ret = [RAConvertor dict2data:retDic];
  4107. }
  4108. return ret;
  4109. }
  4110. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4111. {
  4112. return [self addAll:params from:0];
  4113. }
  4114. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4115. {
  4116. return [self addAll:params from:1];
  4117. }
  4118. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4119. {
  4120. return [self addAll:params from:2];
  4121. }
  4122. #pragma mark - Jack
  4123. #warning 做SQL操作时转义!!
  4124. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4125. // "val_227" : {
  4126. // "check" : 1,
  4127. // "value" : "US United States",
  4128. // "value_id" : "228"
  4129. // },
  4130. if (!countryCode) {
  4131. countryCode = @"US";
  4132. }
  4133. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4134. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4135. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4136. int code_id = sqlite3_column_int(stmt, 3); // id
  4137. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4138. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4139. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4140. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4141. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4142. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4143. }
  4144. long n = *count;
  4145. *count = n + 1;
  4146. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4147. [container setValue:countryDic forKey:key];
  4148. }] mutableCopy];
  4149. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4150. return ret;
  4151. }
  4152. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4153. countryCode = [self translateSingleQuote:countryCode];
  4154. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4155. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4156. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4157. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4158. if (name == NULL) {
  4159. name = "";
  4160. }
  4161. if (code == NULL) {
  4162. code = "";
  4163. }
  4164. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4165. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4166. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4167. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4168. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4169. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4170. }
  4171. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4172. [container setValue:stateDic forKey:key];
  4173. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4174. DebugLog(@"query all state error: %@",err_msg);
  4175. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4176. // [stateDic setValue:@"Other" forKey:@"value"];
  4177. // [stateDic setValue:@"" forKey:@"value_id"];
  4178. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4179. //
  4180. // if (state_code && [@"" isEqualToString:state_code]) {
  4181. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4182. // }
  4183. //
  4184. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4185. // [container setValue:stateDic forKey:key];
  4186. }] mutableCopy];
  4187. [ret removeObjectForKey:@"result"];
  4188. // failure 可以不用了,一样的
  4189. if (ret.allKeys.count == 0) {
  4190. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4191. [stateDic setValue:@"Other" forKey:@"value"];
  4192. [stateDic setValue:@"" forKey:@"value_id"];
  4193. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4194. if (state_code && [@"" isEqualToString:state_code]) {
  4195. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4196. }
  4197. NSString *key = [NSString stringWithFormat:@"val_0"];
  4198. [ret setValue:stateDic forKey:key];
  4199. }
  4200. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4201. return ret;
  4202. }
  4203. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4204. codeId = [self translateSingleQuote:codeId];
  4205. 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];
  4206. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4207. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4208. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4209. if (name == NULL) {
  4210. name = "";
  4211. }
  4212. if (code == NULL) {
  4213. code = "";
  4214. }
  4215. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4216. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4217. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4218. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4219. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4220. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4221. }
  4222. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4223. [container setValue:stateDic forKey:key];
  4224. }] mutableCopy];
  4225. [ret removeObjectForKey:@"result"];
  4226. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4227. return ret;
  4228. }
  4229. + (NSDictionary *)offline_getPrice {
  4230. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4231. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4232. char *name = (char *) sqlite3_column_text(stmt, 1);
  4233. int type = sqlite3_column_int(stmt, 2);
  4234. int orderBy = sqlite3_column_int(stmt, 3);
  4235. if (name == NULL) {
  4236. name = "";
  4237. }
  4238. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4239. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4240. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4241. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4242. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4243. if (orderBy == 0) {
  4244. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4245. }
  4246. [container setValue:priceDic forKey:key];
  4247. }] mutableCopy];
  4248. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4249. return ret;
  4250. }
  4251. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4252. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4253. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4254. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4255. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4256. int _id = sqlite3_column_int(stmt, 0);
  4257. NSString *name = [self textAtColumn:1 statement:stmt];
  4258. NSDictionary *typeDic = @{
  4259. @"value_id" : name,
  4260. @"value" : name,
  4261. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4262. };
  4263. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4264. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4265. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4266. }];
  4267. return ret;
  4268. }
  4269. + (NSDictionary *)offline_getSalesRep {
  4270. // 首先从offline_login表中取出sales_code
  4271. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4272. NSString *user = app.user;
  4273. user = [self translateSingleQuote:user];
  4274. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4275. __block NSString *user_code = @"";
  4276. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4277. char *code = (char *)sqlite3_column_text(stmt, 0);
  4278. if (code == NULL) {
  4279. code = "";
  4280. }
  4281. user_code = [NSString stringWithUTF8String:code];
  4282. }];
  4283. // 再取所有salesRep
  4284. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4285. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4286. // 1 name 2 code 3 salesrep_id
  4287. char *name = (char *)sqlite3_column_text(stmt, 1);
  4288. char *code = (char *)sqlite3_column_text(stmt, 2);
  4289. int salesrep_id = sqlite3_column_int(stmt, 3);
  4290. if (name == NULL) {
  4291. name = "";
  4292. }
  4293. if (code == NULL) {
  4294. code = "";
  4295. }
  4296. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4297. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4298. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4299. // 比较code 相等则check
  4300. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4301. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4302. }
  4303. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4304. }] mutableCopy];
  4305. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4306. return ret;
  4307. }
  4308. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4309. zipcode = [self translateSingleQuote:zipcode];
  4310. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4311. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4312. char *country = (char *)sqlite3_column_text(stmt, 0);
  4313. char *state = (char *)sqlite3_column_text(stmt, 1);
  4314. char *city = (char *)sqlite3_column_text(stmt, 2);
  4315. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4316. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4317. if (country == NULL) {
  4318. country = "";
  4319. }
  4320. if (state == NULL) {
  4321. state = "";
  4322. }
  4323. if (city == NULL) {
  4324. city = "";
  4325. }
  4326. if (country_code == NULL) {
  4327. country_code = "";
  4328. }
  4329. if (state_code == NULL) {
  4330. state_code = "";
  4331. }
  4332. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4333. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4334. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4335. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4336. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4337. }] mutableCopy];
  4338. return ret;
  4339. }
  4340. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4341. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4342. [item setValue:value forKey:valueKey];
  4343. [dic setValue:item forKey:itemKey];
  4344. }
  4345. + (NSString *)countryCodeByid:(NSString *)code_id {
  4346. NSString *ret = nil;
  4347. code_id = [self translateSingleQuote:code_id];
  4348. sqlite3 *db = [iSalesDB get_db];
  4349. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4350. sqlite3_stmt * statement;
  4351. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4352. while (sqlite3_step(statement) == SQLITE_ROW) {
  4353. char *code = (char *)sqlite3_column_text(statement, 0);
  4354. if (code == NULL) {
  4355. code = "";
  4356. }
  4357. ret = [NSString stringWithUTF8String:code];
  4358. }
  4359. sqlite3_finalize(statement);
  4360. }
  4361. [iSalesDB close_db:db];
  4362. return ret;
  4363. }
  4364. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4365. NSString *ret = nil;
  4366. code = [self translateSingleQuote:code];
  4367. sqlite3 *db = [iSalesDB get_db];
  4368. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4369. sqlite3_stmt * statement;
  4370. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4371. while (sqlite3_step(statement) == SQLITE_ROW) {
  4372. char *_id = (char *)sqlite3_column_text(statement, 0);
  4373. if (_id == NULL) {
  4374. _id = "";
  4375. }
  4376. ret = [NSString stringWithFormat:@"%s",_id];
  4377. }
  4378. sqlite3_finalize(statement);
  4379. }
  4380. [iSalesDB close_db:db];
  4381. return ret;
  4382. }
  4383. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4384. NSString *name = nil;
  4385. codeId = [self translateSingleQuote:codeId];
  4386. sqlite3 *db = [iSalesDB get_db];
  4387. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4388. sqlite3_stmt * statement;
  4389. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4390. while (sqlite3_step(statement) == SQLITE_ROW) {
  4391. char *value = (char *)sqlite3_column_text(statement, 0);
  4392. if (value == NULL) {
  4393. value = "";
  4394. }
  4395. name = [NSString stringWithUTF8String:value];
  4396. }
  4397. sqlite3_finalize(statement);
  4398. }
  4399. [iSalesDB close_db:db];
  4400. return name;
  4401. }
  4402. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4403. NSString *ret = nil;
  4404. sqlite3 *db = [iSalesDB get_db];
  4405. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4406. sqlite3_stmt * statement;
  4407. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4408. while (sqlite3_step(statement) == SQLITE_ROW) {
  4409. char *name = (char *)sqlite3_column_text(statement, 0);
  4410. if (name == NULL) {
  4411. name = "";
  4412. }
  4413. ret = [NSString stringWithUTF8String:name];
  4414. }
  4415. sqlite3_finalize(statement);
  4416. }
  4417. [iSalesDB close_db:db];
  4418. return ret;
  4419. }
  4420. + (NSString *)salesRepCodeById:(NSString *)_id {
  4421. NSString *ret = nil;
  4422. _id = [self translateSingleQuote:_id];
  4423. sqlite3 *db = [iSalesDB get_db];
  4424. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4425. sqlite3_stmt * statement;
  4426. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4427. while (sqlite3_step(statement) == SQLITE_ROW) {
  4428. char *rep = (char *)sqlite3_column_text(statement, 0);
  4429. if (rep == NULL) {
  4430. rep = "";
  4431. }
  4432. ret = [NSString stringWithUTF8String:rep];
  4433. }
  4434. sqlite3_finalize(statement);
  4435. }
  4436. [iSalesDB close_db:db];
  4437. return ret;
  4438. }
  4439. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4440. char *tx = (char *)sqlite3_column_text(stmt, col);
  4441. if (tx == NULL) {
  4442. tx = "";
  4443. }
  4444. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4445. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4446. if (!text) {
  4447. text = @"";
  4448. }
  4449. // 将字符全部为' '的字符串干掉
  4450. int spaceCount = 0;
  4451. for (int i = 0; i < text.length; i++) {
  4452. if ([text characterAtIndex:i] == ' ') {
  4453. spaceCount++;
  4454. }
  4455. }
  4456. if (spaceCount == text.length) {
  4457. text = @"";
  4458. }
  4459. return text;
  4460. }
  4461. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4462. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4463. NSData *data = [NSData dataWithContentsOfFile:path];
  4464. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4465. return ret;
  4466. }
  4467. + (NSString *)textFileName:(NSString *)name {
  4468. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4469. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4470. if (!text) {
  4471. text = @"";
  4472. }
  4473. return text;
  4474. }
  4475. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4476. return [[dic objectForKey:key] mutableCopy];
  4477. }
  4478. + (id)translateSingleQuote:(NSString *)string {
  4479. if ([string isKindOfClass:[NSString class]])
  4480. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4481. return string;
  4482. }
  4483. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4484. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4485. }
  4486. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4487. NSString* ret= nil;
  4488. NSString *sqlQuery = nil;
  4489. // 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
  4490. 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];
  4491. sqlite3_stmt * statement;
  4492. // int count=0;
  4493. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4494. {
  4495. if (sqlite3_step(statement) == SQLITE_ROW)
  4496. {
  4497. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4498. if(imgurl==nil)
  4499. imgurl="";
  4500. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4501. ret=nsimgurl;
  4502. }
  4503. sqlite3_finalize(statement);
  4504. }
  4505. else
  4506. {
  4507. [ret setValue:@"8" forKey:@"result"];
  4508. }
  4509. // [iSalesDB close_db:db];
  4510. // DebugLog(@"data string: %@",ret );
  4511. return ret;
  4512. }
  4513. #pragma mark contact list
  4514. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4515. {
  4516. // contactType = "Sales_Order_Customer";
  4517. // limit = 25;
  4518. // offset = 0;
  4519. // password = 123456;
  4520. // "price_name" = 16;
  4521. // user = EvanK;
  4522. sqlite3 *db = [iSalesDB get_db];
  4523. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4524. if (contactType) {
  4525. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4526. } else {
  4527. contactType = @"1 = 1";
  4528. }
  4529. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4530. DebugLog(@"offline contact list keyword: %@",keyword);
  4531. // advanced search
  4532. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4533. if (contact_name) {
  4534. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4535. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4536. } else {
  4537. contact_name = @"";
  4538. }
  4539. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4540. if (customer_phone) {
  4541. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4542. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4543. } else {
  4544. customer_phone = @"";
  4545. }
  4546. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4547. if (customer_fax) {
  4548. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4549. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4550. } else {
  4551. customer_fax = @"";
  4552. }
  4553. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4554. if (customer_zipcode) {
  4555. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4556. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4557. } else {
  4558. customer_zipcode = @"";
  4559. }
  4560. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4561. if (customer_sales_rep) {
  4562. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4563. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4564. } else {
  4565. customer_sales_rep = @"";
  4566. }
  4567. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4568. if (customer_state) {
  4569. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4570. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4571. } else {
  4572. customer_state = @"";
  4573. }
  4574. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4575. if (customer_name) {
  4576. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4577. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4578. } else {
  4579. customer_name = @"";
  4580. }
  4581. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4582. if (customer_country) {
  4583. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4584. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4585. } else {
  4586. customer_country = @"";
  4587. }
  4588. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4589. if (customer_cid) {
  4590. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4591. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4592. } else {
  4593. customer_cid = @"";
  4594. }
  4595. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4596. if (customer_city) {
  4597. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4598. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4599. } else {
  4600. customer_city = @"";
  4601. }
  4602. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4603. if (customer_address) {
  4604. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4605. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4606. } else {
  4607. customer_address = @"";
  4608. }
  4609. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4610. if (customer_email) {
  4611. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4612. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4613. } else {
  4614. customer_email = @"";
  4615. }
  4616. NSString *price_name = [params valueForKey:@"price_name"];
  4617. if (price_name) {
  4618. if ([price_name containsString:@","]) {
  4619. // 首先从 price表中查处name
  4620. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4621. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4622. for (int i = 1;i < pArray.count;i++) {
  4623. NSString *p = pArray[i];
  4624. [mutablePStr appendFormat:@" or type = %@ ",p];
  4625. }
  4626. [mutablePStr appendString:@";"];
  4627. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4628. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4629. char *name = (char *)sqlite3_column_text(stmt, 0);
  4630. if (!name)
  4631. name = "";
  4632. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4633. }];
  4634. // 再根据name 拼sql
  4635. NSMutableString *mutable_price_name = [NSMutableString string];
  4636. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4637. for (int i = 1; i < price_name_array.count; i++) {
  4638. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4639. }
  4640. [mutable_price_name appendString:@")"];
  4641. price_name = mutable_price_name;
  4642. } else {
  4643. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4644. if ([price_name isEqualToString:@""]) {
  4645. price_name = @"";
  4646. } else {
  4647. __block NSString *price;
  4648. price_name = [self translateSingleQuote:price_name];
  4649. [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) {
  4650. char *p = (char *)sqlite3_column_text(stmt, 0);
  4651. if (p == NULL) {
  4652. p = "";
  4653. }
  4654. price = [NSString stringWithUTF8String:p];
  4655. }];
  4656. if ([price isEqualToString:@""]) {
  4657. price_name = @"";
  4658. } else {
  4659. price = [self translateSingleQuote:price];
  4660. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4661. }
  4662. }
  4663. }
  4664. } else {
  4665. price_name = @"";
  4666. }
  4667. int limit = [[params valueForKey:@"limit"] intValue];
  4668. int offset = [[params valueForKey:@"offset"] intValue];
  4669. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4670. 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];
  4671. 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];
  4672. // int result= [iSalesDB AddExFunction:db];
  4673. int count =0;
  4674. NSString *sqlQuery = nil;
  4675. if(keyword.length==0)
  4676. {
  4677. // 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];
  4678. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4679. sqlQuery = sql;
  4680. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4681. }
  4682. else
  4683. {
  4684. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4685. keyword = keyword.lowercaseString;
  4686. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4687. 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];
  4688. 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]];
  4689. }
  4690. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4691. sqlite3_stmt * statement;
  4692. [ret setValue:@"2" forKey:@"result"];
  4693. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4694. // sqlQuery = @"select editable,decrypt(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 Sales_Order_Customer = 1 and is_active=1 order by decrypt(company_name) limit 25 offset 0";
  4695. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4696. {
  4697. int i = 0;
  4698. while (sqlite3_step(statement) == SQLITE_ROW)
  4699. {
  4700. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4701. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4702. int editable = sqlite3_column_int(statement, 0);
  4703. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4704. NSString *nscompany_name =nil;
  4705. if(company_name==nil)
  4706. nscompany_name=@"";
  4707. else
  4708. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4709. char *country = (char*)sqlite3_column_text(statement, 2);
  4710. if(country==nil)
  4711. country="";
  4712. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4713. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4714. // if(addr==nil)
  4715. // addr="";
  4716. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4717. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4718. if(zipcode==nil)
  4719. zipcode="";
  4720. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4721. char *state = (char*)sqlite3_column_text(statement, 5);
  4722. if(state==nil)
  4723. state="";
  4724. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4725. char *city = (char*)sqlite3_column_text(statement, 6);
  4726. if(city==nil)
  4727. city="";
  4728. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4729. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4730. // NSString *nscontact_name = nil;
  4731. // if(contact_name==nil)
  4732. // nscontact_name=@"";
  4733. // else
  4734. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4735. char *phone = (char*)sqlite3_column_text(statement, 8);
  4736. NSString *nsphone = nil;
  4737. if(phone==nil)
  4738. nsphone=@"";
  4739. else
  4740. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4741. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4742. if(contact_id==nil)
  4743. contact_id="";
  4744. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4745. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4746. if(addr_1==nil)
  4747. addr_1="";
  4748. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4749. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4750. if(addr_2==nil)
  4751. addr_2="";
  4752. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4753. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4754. if(addr_3==nil)
  4755. addr_3="";
  4756. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4757. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4758. if(addr_4==nil)
  4759. addr_4="";
  4760. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4761. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4762. if(first_name==nil)
  4763. first_name="";
  4764. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4765. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4766. if(last_name==nil)
  4767. last_name="";
  4768. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4769. char *fax = (char*)sqlite3_column_text(statement, 16);
  4770. NSString *nsfax = nil;
  4771. if(fax==nil)
  4772. nsfax=@"";
  4773. else
  4774. {
  4775. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4776. if(nsfax.length>0)
  4777. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4778. }
  4779. char *email = (char*)sqlite3_column_text(statement, 17);
  4780. NSString *nsemail = nil;
  4781. if(email==nil)
  4782. nsemail=@"";
  4783. else
  4784. {
  4785. nsemail= [[NSString alloc]initWithUTF8String:email];
  4786. if(nsemail.length>0)
  4787. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4788. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4789. }
  4790. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4791. [arr_name addObject:nsfirst_name];
  4792. [arr_name addObject:nslast_name];
  4793. NSString *nscontact_name=[RAConvertor arr2string:arr_name separator:@" " trim:true];
  4794. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4795. {
  4796. // decrypt
  4797. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4798. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4799. nsphone=[AESCrypt fastdecrypt:nsphone];
  4800. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4801. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4802. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4803. }
  4804. [arr_addr addObject:nscompany_name];
  4805. [arr_addr addObject:nscontact_name];
  4806. [arr_addr addObject:@"<br>"];
  4807. [arr_addr addObject:nsaddr_1];
  4808. [arr_addr addObject:nsaddr_2];
  4809. [arr_addr addObject:nsaddr_3];
  4810. [arr_addr addObject:nsaddr_4];
  4811. //[arr_addr addObject:nsaddr];
  4812. [arr_addr addObject:nszipcode];
  4813. [arr_addr addObject:nscity];
  4814. [arr_addr addObject:nsstate];
  4815. [arr_addr addObject:nscountry];
  4816. [arr_addr addObject:@"<br>"];
  4817. [arr_addr addObject:nsphone];
  4818. [arr_addr addObject:nsfax];
  4819. [arr_addr addObject:nsemail];
  4820. NSString * name = [RAConvertor arr2string:arr_addr separator:@", " trim:true];
  4821. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4822. [item setValue:name forKey:@"name"];
  4823. [item setValue:nscontact_id forKey:@"contact_id"];
  4824. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4825. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4826. i++;
  4827. }
  4828. UIApplication * app = [UIApplication sharedApplication];
  4829. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4830. [ret setValue:appDelegate.mode forKey:@"mode"];
  4831. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4832. sqlite3_finalize(statement);
  4833. }
  4834. [iSalesDB close_db:db];
  4835. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  4836. return ret;
  4837. }
  4838. #pragma mark contact Advanced search
  4839. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4840. {
  4841. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4842. return [RAConvertor dict2data:contactAdvanceDic];
  4843. }
  4844. #pragma mark create new contact
  4845. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4846. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4847. NSData *data = [NSData dataWithContentsOfFile:path];
  4848. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4849. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4850. NSString *countryCode = nil;
  4851. NSString *countryCode_id = nil;
  4852. NSString *stateCode = nil;
  4853. NSString *city = nil;
  4854. NSString *zipCode = nil;
  4855. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4856. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4857. NSString *code_id = params[@"country"];
  4858. countryCode_id = code_id;
  4859. countryCode = [self countryCodeByid:code_id];
  4860. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4861. NSString *zip_code = params[@"zipcode"];
  4862. // 剔除全部为空格
  4863. int spaceCount = 0;
  4864. for (int i = 0; i < zip_code.length; i++) {
  4865. if ([zip_code characterAtIndex:i] == ' ') {
  4866. spaceCount++;
  4867. }
  4868. }
  4869. if (spaceCount == zip_code.length) {
  4870. zip_code = @"";
  4871. }
  4872. zipCode = zip_code;
  4873. if (zipCode.length > 0) {
  4874. countryCode_id = params[@"country"];
  4875. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4876. countryCode = [dic valueForKey:@"country_code"];
  4877. if (!countryCode) {
  4878. // countryCode = @"US";
  4879. NSString *code_id = params[@"country"];
  4880. countryCode = [self countryCodeByid:code_id];
  4881. }
  4882. stateCode = [dic valueForKey:@"state_code"];
  4883. if (!stateCode.length) {
  4884. stateCode = params[@"state"];
  4885. }
  4886. city = [dic valueForKey:@"city"];
  4887. if (!city.length) {
  4888. city = params[@"city"];
  4889. }
  4890. // zip code
  4891. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4892. [zipDic setValue:zipCode forKey:@"value"];
  4893. [section_0 setValue:zipDic forKey:@"item_11"];
  4894. } else {
  4895. NSString *code_id = params[@"country"];
  4896. countryCode = [self countryCodeByid:code_id];
  4897. stateCode = params[@"state"];
  4898. city = params[@"city"];
  4899. }
  4900. }
  4901. } else {
  4902. // default: US United States
  4903. countryCode = @"US";
  4904. countryCode_id = @"228";
  4905. }
  4906. // country
  4907. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4908. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4909. // state
  4910. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4911. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4912. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4913. // city
  4914. if (city) {
  4915. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4916. [cityDic setValue:city forKey:@"value"];
  4917. [section_0 setValue:cityDic forKey:@"item_13"];
  4918. }
  4919. // price type
  4920. NSDictionary *priceDic = [self offline_getPrice];
  4921. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4922. // contact type
  4923. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4924. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4925. // Sales Rep
  4926. NSDictionary *repDic = [self offline_getSalesRep];
  4927. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4928. [ret setValue:section_0 forKey:@"section_0"];
  4929. return [RAConvertor dict2data:ret];
  4930. }
  4931. #pragma mark save
  4932. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4933. NSString *addr = nil;
  4934. NSString *contact_name = nil;
  4935. NSString *companyName = [params objectForKey:@"company"];
  4936. if (companyName) {
  4937. companyName = [AESCrypt fastencrypt:companyName];
  4938. } else {
  4939. companyName = @"";
  4940. }
  4941. DebugLog(@"company");
  4942. companyName = [self translateSingleQuote:companyName];
  4943. NSString *addr1 = [params objectForKey:@"address"];
  4944. NSString *addr2 = [params objectForKey:@"address2"];
  4945. NSString *addr3 = [params objectForKey:@"address_3"];
  4946. NSString *addr4 = [params objectForKey:@"address_4"];
  4947. if (!addr2) {
  4948. addr2 = @"";
  4949. }
  4950. if (!addr3) {
  4951. addr3 = @"";
  4952. }
  4953. if (!addr4) {
  4954. addr4 = @"";
  4955. }
  4956. if (!addr1) {
  4957. addr1 = @"";
  4958. }
  4959. DebugLog(@"addr");
  4960. addr = [RAConvertor arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4961. addr = [AESCrypt fastencrypt:addr];
  4962. addr = [self translateSingleQuote:addr];
  4963. if (addr1 && ![addr1 isEqualToString:@""]) {
  4964. addr1 = [AESCrypt fastencrypt:addr1];
  4965. }
  4966. addr1 = [self translateSingleQuote:addr1];
  4967. addr2 = [self translateSingleQuote:addr2];
  4968. addr3 = [self translateSingleQuote:addr3];
  4969. addr4 = [self translateSingleQuote:addr4];
  4970. NSString *country = [params objectForKey:@"country"];
  4971. if (country) {
  4972. country = [self countryNameByCountryCodeId:country];
  4973. } else {
  4974. country = @"";
  4975. }
  4976. DebugLog(@"country");
  4977. country = [self translateSingleQuote:country];
  4978. NSString *state = [params objectForKey:@"state"];
  4979. if (!state) {
  4980. state = @"";
  4981. }
  4982. DebugLog(@"state");
  4983. state = [self translateSingleQuote:state];
  4984. NSString *city = [params objectForKey:@"city"];
  4985. if (!city) {
  4986. city = @"";
  4987. }
  4988. city = [self translateSingleQuote:city];
  4989. NSString *zipcode = [params objectForKey:@"zipcode"];
  4990. if (!zipcode) {
  4991. zipcode = @"";
  4992. }
  4993. DebugLog(@"zip");
  4994. zipcode = [self translateSingleQuote:zipcode];
  4995. NSString *fistName = [params objectForKey:@"firstname"];
  4996. if (!fistName) {
  4997. fistName = @"";
  4998. }
  4999. NSString *lastName = [params objectForKey:@"lastname"];
  5000. if (!lastName) {
  5001. lastName = @"";
  5002. }
  5003. contact_name = [RAConvertor arr2string:@[fistName,lastName] separator:@" " trim:true];
  5004. DebugLog(@"contact_name");
  5005. contact_name = [self translateSingleQuote:contact_name];
  5006. fistName = [self translateSingleQuote:fistName];
  5007. lastName = [self translateSingleQuote:lastName];
  5008. NSString *phone = [params objectForKey:@"phone"];
  5009. if (phone) {
  5010. phone = [AESCrypt fastencrypt:phone];
  5011. } else {
  5012. phone = @"";
  5013. }
  5014. DebugLog(@"PHONE");
  5015. phone = [self translateSingleQuote:phone];
  5016. NSString *fax = [params objectForKey:@"fax"];
  5017. if (!fax) {
  5018. fax = @"";
  5019. }
  5020. DebugLog(@"FAX");
  5021. fax = [self translateSingleQuote:fax];
  5022. NSString *email = [params objectForKey:@"email"];
  5023. if (!email) {
  5024. email = @"";
  5025. }
  5026. DebugLog(@"EMAIL:%@",email);
  5027. email = [self translateSingleQuote:email];
  5028. NSString *notes = [params objectForKey:@"contact_notes"];
  5029. if (!notes) {
  5030. notes = @"";
  5031. }
  5032. DebugLog(@"NOTE:%@",notes);
  5033. notes = [self translateSingleQuote:notes];
  5034. NSString *price = [params objectForKey:@"price_name"];
  5035. if (price) {
  5036. price = [self priceNameByPriceId:price];
  5037. } else {
  5038. price = @"";
  5039. }
  5040. DebugLog(@"PRICE");
  5041. price = [self translateSingleQuote:price];
  5042. NSString *salesRep = [params objectForKey:@"sales_rep"];
  5043. if (salesRep) {
  5044. salesRep = [self salesRepCodeById:salesRep];
  5045. } else {
  5046. salesRep = @"";
  5047. }
  5048. salesRep = [self translateSingleQuote:salesRep];
  5049. NSString *img = [params objectForKey:@"business_card"];
  5050. NSArray *array = [img componentsSeparatedByString:@","];
  5051. NSString *img_0 = array[0];
  5052. if (!img_0) {
  5053. img_0 = @"";
  5054. }
  5055. img_0 = [self translateSingleQuote:img_0];
  5056. NSString *img_1 = array[1];
  5057. if (!img_1) {
  5058. img_1 = @"";
  5059. }
  5060. img_1 = [self translateSingleQuote:img_1];
  5061. NSString *img_2 = array[2];
  5062. if (!img_2) {
  5063. img_2 = @"";
  5064. }
  5065. img_2 = [self translateSingleQuote:img_2];
  5066. NSString *contact_id = [NSUUID UUID].UUIDString;
  5067. NSString *contact_type = [params objectForKey:@"type_name"];
  5068. if (!contact_type) {
  5069. contact_type = @"";
  5070. }
  5071. contact_type = [self translateSingleQuote:contact_type];
  5072. // 判断更新时是否为customer
  5073. if (update) {
  5074. contact_id = [params objectForKey:@"contact_id"];
  5075. if (!contact_id) {
  5076. contact_id = @"";
  5077. }
  5078. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5079. __block int customer = 0;
  5080. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5081. customer = sqlite3_column_int(stmt, 0);
  5082. }];
  5083. isCustomer = customer ? YES : NO;
  5084. }
  5085. NSMutableDictionary *sync_dic = [params mutableCopy];
  5086. if (isCustomer) {
  5087. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5088. } else {
  5089. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5090. }
  5091. NSString *sync_data = nil;
  5092. NSString *sql = nil;
  5093. if (update){
  5094. contact_id = [params objectForKey:@"contact_id"];
  5095. if (!contact_id) {
  5096. contact_id = @"";
  5097. }
  5098. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5099. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5100. sync_data = [RAConvertor dict2string:sync_dic];
  5101. sync_data = [self translateSingleQuote:sync_data];
  5102. 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 = '%@',type = '%@' 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_type,contact_id];
  5103. } else {
  5104. contact_id = [self translateSingleQuote:contact_id];
  5105. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5106. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5107. sync_data = [RAConvertor dict2string:sync_dic];
  5108. sync_data = [self translateSingleQuote:sync_data];
  5109. 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,type) 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,contact_type];
  5110. }
  5111. int result = [iSalesDB execSql:sql];
  5112. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5113. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5114. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5115. }
  5116. #pragma mark save new contact
  5117. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5118. {
  5119. return [self offline_saveContact:params update:NO isCustomer:YES];
  5120. }
  5121. #pragma mark edit contact
  5122. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5123. {
  5124. // {
  5125. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5126. // password = 123456;
  5127. // user = EvanK;
  5128. // }
  5129. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5130. NSData *data = [NSData dataWithContentsOfFile:path];
  5131. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5132. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5133. NSString *countryCode = nil;
  5134. NSString *countryCode_id = nil;
  5135. NSString *stateCode = nil;
  5136. /*------contact infor------*/
  5137. __block NSString *country = nil;
  5138. __block NSString *company_name = nil;
  5139. __block NSString *contact_id = params[@"contact_id"];
  5140. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5141. __block NSString *zipcode = nil;
  5142. __block NSString *state = nil; // state_code
  5143. __block NSString *city = nil; //
  5144. __block NSString *firt_name,*last_name;
  5145. __block NSString *phone,*fax,*email;
  5146. __block NSString *notes,*price_type,*sales_rep;
  5147. __block NSString *img_0,*img_1,*img_2;
  5148. __block NSString *contact_type;
  5149. contact_id = [self translateSingleQuote:contact_id];
  5150. 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,type from offline_contact where contact_id = '%@';",contact_id];
  5151. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5152. country = [self textAtColumn:0 statement:stmt]; // country name
  5153. company_name = [self textAtColumn:1 statement:stmt];
  5154. addr_1 = [self textAtColumn:2 statement:stmt];
  5155. addr_2 = [self textAtColumn:3 statement:stmt];
  5156. addr_3 = [self textAtColumn:4 statement:stmt];
  5157. addr_4 = [self textAtColumn:5 statement:stmt];
  5158. zipcode = [self textAtColumn:6 statement:stmt];
  5159. state = [self textAtColumn:7 statement:stmt]; // state code
  5160. city = [self textAtColumn:8 statement:stmt];
  5161. firt_name = [self textAtColumn:9 statement:stmt];
  5162. last_name = [self textAtColumn:10 statement:stmt];
  5163. phone = [self textAtColumn:11 statement:stmt];
  5164. fax = [self textAtColumn:12 statement:stmt];
  5165. email = [self textAtColumn:13 statement:stmt];
  5166. notes = [self textAtColumn:14 statement:stmt];
  5167. price_type = [self textAtColumn:15 statement:stmt]; // name
  5168. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5169. img_0 = [self textAtColumn:17 statement:stmt];
  5170. img_1 = [self textAtColumn:18 statement:stmt];
  5171. img_2 = [self textAtColumn:19 statement:stmt];
  5172. contact_type = [self textAtColumn:20 statement:stmt];
  5173. }];
  5174. // decrypt
  5175. if (company_name) {
  5176. company_name = [AESCrypt fastdecrypt:company_name];
  5177. }
  5178. if (addr_1) {
  5179. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5180. }
  5181. if (phone) {
  5182. phone = [AESCrypt fastdecrypt:phone];
  5183. }
  5184. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5185. countryCode = [iSalesDB jk_queryText:countrySql];
  5186. stateCode = state;
  5187. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5188. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5189. NSString *code_id = params[@"country"];
  5190. countryCode_id = code_id;
  5191. countryCode = [self countryCodeByid:code_id];
  5192. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5193. NSString *zip_code = params[@"zipcode"];
  5194. // 剔除全部为空格
  5195. int spaceCount = 0;
  5196. for (int i = 0; i < zip_code.length; i++) {
  5197. if ([zip_code characterAtIndex:i] == ' ') {
  5198. spaceCount++;
  5199. }
  5200. }
  5201. if (spaceCount == zip_code.length) {
  5202. zip_code = @"";
  5203. }
  5204. if (zipcode.length > 0) {
  5205. zipcode = zip_code;
  5206. countryCode_id = params[@"country"];
  5207. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5208. countryCode = [dic valueForKey:@"country_code"];
  5209. if (!countryCode) {
  5210. NSString *code_id = params[@"country"];
  5211. countryCode = [self countryCodeByid:code_id];
  5212. }
  5213. stateCode = [dic valueForKey:@"state_code"];
  5214. if (!stateCode.length) {
  5215. stateCode = params[@"state"];
  5216. }
  5217. city = [dic valueForKey:@"city"];
  5218. if (!city.length) {
  5219. city = params[@"city"];
  5220. }
  5221. // zip code
  5222. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5223. [zipDic setValue:zipcode forKey:@"value"];
  5224. [section_0 setValue:zipDic forKey:@"item_8"];
  5225. } else {
  5226. NSString *code_id = params[@"country"];
  5227. countryCode = [self countryCodeByid:code_id];
  5228. stateCode = params[@"state"];
  5229. city = params[@"city"];
  5230. }
  5231. }
  5232. }
  5233. // 0 Country
  5234. // 1 Company Name
  5235. // 2 Contact ID
  5236. // 3 Picture
  5237. // 4 Address 1
  5238. // 5 Address 2
  5239. // 6 Address 3
  5240. // 7 Address 4
  5241. // 8 Zip Code
  5242. // 9 State/Province
  5243. // 10 City
  5244. // 11 Contact First Name
  5245. // 12 Contact Last Name
  5246. // 13 Phone
  5247. // 14 Fax
  5248. // 15 Email
  5249. // 16 Contact Notes
  5250. // 17 Price Type
  5251. // 18 Contact Type
  5252. // 19 Sales Rep
  5253. // country
  5254. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5255. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5256. // company
  5257. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5258. // contact_id
  5259. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5260. // picture
  5261. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5262. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5263. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5264. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5265. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5266. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5267. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5268. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5269. // addr 1 2 3 4
  5270. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5271. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5272. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5273. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5274. // zip code
  5275. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5276. // state
  5277. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5278. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5279. // city
  5280. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5281. // first last
  5282. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5283. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5284. // phone fax email
  5285. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5286. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5287. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5288. // notes
  5289. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5290. // price
  5291. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5292. for (NSString *key in priceDic.allKeys) {
  5293. if ([key containsString:@"val_"]) {
  5294. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5295. if ([dic[@"value"] isEqualToString:price_type]) {
  5296. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5297. [priceDic setValue:dic forKey:key];
  5298. }
  5299. }
  5300. }
  5301. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5302. // Contact Rep
  5303. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5304. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5305. // Sales Rep
  5306. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5307. for (NSString *key in repDic.allKeys) {
  5308. if ([key containsString:@"val_"]) {
  5309. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5310. NSString *value = dic[@"value"];
  5311. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5312. if (code && [code isEqualToString:sales_rep]) {
  5313. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5314. [repDic setValue:dic forKey:key];
  5315. }
  5316. }
  5317. }
  5318. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5319. [ret setValue:section_0 forKey:@"section_0"];
  5320. return [RAConvertor dict2data:ret];
  5321. }
  5322. #pragma mark save contact
  5323. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5324. {
  5325. return [self offline_saveContact:params update:YES isCustomer:YES];
  5326. }
  5327. #pragma mark category
  5328. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5329. if (ck) {
  5330. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5331. for (NSString *key in res.allKeys) {
  5332. if (![key isEqualToString:@"count"]) {
  5333. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5334. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5335. if ([val[@"value"] isEqualToString:ck]) {
  5336. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5337. }
  5338. [res setValue:val forKey:key];
  5339. }
  5340. }
  5341. [dic setValue:res forKey:valueKey];
  5342. }
  5343. }
  5344. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5345. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5346. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5347. NSString* category = [params valueForKey:@"category"];
  5348. if (!category || [category isEqualToString:@""]) {
  5349. category = @"%";
  5350. }
  5351. category = [self translateSingleQuote:category];
  5352. int limit = [[params valueForKey:@"limit"] intValue];
  5353. int offset = [[params valueForKey:@"offset"] intValue];
  5354. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5355. NSString *limit_str = @"";
  5356. if (limited) {
  5357. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5358. }
  5359. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5360. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5361. sqlite3 *db = [iSalesDB get_db];
  5362. // [iSalesDB AddExFunction:db];
  5363. int count;
  5364. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5365. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color 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];
  5366. double price_min = 0;
  5367. double price_max = 0;
  5368. if ([params.allKeys containsObject:@"alert"]) {
  5369. // alert
  5370. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5371. NSString *alert = params[@"alert"];
  5372. if ([alert isEqualToString:@"Display All"]) {
  5373. alert = [NSString stringWithFormat:@""];
  5374. } else {
  5375. alert = [self translateSingleQuote:alert];
  5376. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5377. }
  5378. // available
  5379. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5380. NSString *available = params[@"available"];
  5381. NSString *available_condition;
  5382. if ([available isEqualToString:@"Display All"]) {
  5383. available_condition = @"";
  5384. } else if ([available isEqualToString:@"Available Now"]) {
  5385. available_condition = @"and availability > 0";
  5386. } else {
  5387. available_condition = @"and availability == 0";
  5388. }
  5389. // best seller
  5390. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5391. NSString *best_seller = @"";
  5392. NSString *order_best_seller = @"m.name asc";
  5393. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5394. best_seller = @"and best_seller > 0";
  5395. order_best_seller = @"m.best_seller desc,m.name asc";
  5396. }
  5397. // price
  5398. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5399. NSString *price = params[@"price"];
  5400. price_min = 0;
  5401. price_max = MAXFLOAT;
  5402. if (appDelegate.user && price != nil) {
  5403. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5404. NSMutableString *priceName = [NSMutableString string];
  5405. for (int i = 0; i < priceTypeArray.count; i++) {
  5406. NSString *pricetype = priceTypeArray[i];
  5407. pricetype = [self translateSingleQuote:pricetype];
  5408. if (i == 0) {
  5409. [priceName appendFormat:@"'%@'",pricetype];
  5410. } else {
  5411. [priceName appendFormat:@",'%@'",pricetype];
  5412. }
  5413. }
  5414. if ([price isEqualToString:@"Display All"]) {
  5415. price = [NSString stringWithFormat:@""];
  5416. } else if([price containsString:@"+"]){
  5417. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5418. price_min = [price doubleValue];
  5419. 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];
  5420. } else {
  5421. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5422. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5423. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5424. 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];
  5425. }
  5426. } else {
  5427. price = @"";
  5428. }
  5429. // sold_by_qty : Sold in quantities of %@
  5430. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5431. NSString *qty = params[@"sold_by_qty"];
  5432. if ([qty isEqualToString:@"Display All"]) {
  5433. qty = @"";
  5434. } else {
  5435. qty = [self translateSingleQuote:qty];
  5436. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5437. }
  5438. // cate
  5439. // category = [self translateSingleQuote:category];
  5440. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5441. // cate mutiple selection
  5442. NSString *category_id = params[@"category"];
  5443. NSMutableArray *cate_id_array = nil;
  5444. NSMutableString *cateWhere = [NSMutableString string];
  5445. if ([category_id isEqualToString:@""] || !category_id) {
  5446. [cateWhere appendString:@"1 = 1"];
  5447. } else {
  5448. if ([category_id containsString:@","]) {
  5449. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5450. } else {
  5451. cate_id_array = [@[category_id] mutableCopy];
  5452. }
  5453. [cateWhere appendString:@"("];
  5454. for (int i = 0; i < cate_id_array.count; i++) {
  5455. if (i == 0) {
  5456. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5457. } else {
  5458. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5459. }
  5460. }
  5461. [cateWhere appendString:@")"];
  5462. }
  5463. // where bestseller > 0 order by bestseller desc
  5464. // sql query: alert availability(int) best_seller(int) price qty
  5465. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from (select name,description,product_id,closeout,best_seller,category,more_color 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];
  5466. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5467. }
  5468. DebugLog(@"offline category where: %@",where);
  5469. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5470. if (!appDelegate.user) {
  5471. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5472. }
  5473. [ret setValue:filter forKey:@"filter"];
  5474. DebugLog(@"offline_category sql:%@",sqlQuery);
  5475. sqlite3_stmt * statement;
  5476. [ret setValue:@"2" forKey:@"result"];
  5477. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5478. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5479. // int count=0;
  5480. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5481. {
  5482. int i=0;
  5483. while (sqlite3_step(statement) == SQLITE_ROW)
  5484. {
  5485. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5486. char *name = (char*)sqlite3_column_text(statement, 0);
  5487. if(name==nil)
  5488. name="";
  5489. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5490. char *description = (char*)sqlite3_column_text(statement, 1);
  5491. if(description==nil)
  5492. description="";
  5493. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5494. int product_id = sqlite3_column_int(statement, 2);
  5495. int wid = sqlite3_column_int(statement, 3);
  5496. int closeout = sqlite3_column_int(statement, 4);
  5497. int cid = sqlite3_column_int(statement, 5);
  5498. int wisdelete = sqlite3_column_int(statement, 6);
  5499. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5500. int more_color = sqlite3_column_int(statement, 8);
  5501. // Defaul Category ID
  5502. __block NSString *categoryID = nil;
  5503. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5504. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5505. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5506. if(default_category==nil)
  5507. default_category="";
  5508. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5509. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5510. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5511. categoryID = nsdefault_category;
  5512. }];
  5513. if (!categoryID.length) {
  5514. NSString *cateIDs = params[@"category"];
  5515. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5516. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5517. for (NSString *cateID in requestCategoryArr) {
  5518. BOOL needBreak = NO;
  5519. for (NSString *itemCateIDBox in itemCategoryArr) {
  5520. if (itemCateIDBox.length > 4) {
  5521. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5522. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5523. if ([itemCategoryID isEqualToString:cateID]) {
  5524. needBreak = YES;
  5525. categoryID = itemCategoryID;
  5526. break;
  5527. }
  5528. }
  5529. }
  5530. if (needBreak) {
  5531. break;
  5532. }
  5533. }
  5534. }
  5535. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5536. if(wid !=0 && wisdelete != 1)
  5537. [item setValue:@"true" forKey:@"wish_exists"];
  5538. else
  5539. [item setValue:@"false" forKey:@"wish_exists"];
  5540. if(closeout==0)
  5541. [item setValue:@"false" forKey:@"is_closeout"];
  5542. else
  5543. [item setValue:@"true" forKey:@"is_closeout"];
  5544. if(cid==0)
  5545. [item setValue:@"false" forKey:@"cart_exists"];
  5546. else
  5547. [item setValue:@"true" forKey:@"cart_exists"];
  5548. if (more_color == 0) {
  5549. [item setObject:@(false) forKey:@"more_color"];
  5550. } else if (more_color == 1) {
  5551. [item setObject:@(true) forKey:@"more_color"];
  5552. }
  5553. [item addEntriesFromDictionary:imgjson];
  5554. // [item setValue:nsurl forKey:@"img"];
  5555. [item setValue:nsname forKey:@"name"];
  5556. [item setValue:nsdescription forKey:@"description"];
  5557. if (categoryID) {
  5558. [item setValue:categoryID forKey:@"item_category_id"];
  5559. }
  5560. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5561. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5562. i++;
  5563. }
  5564. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5565. [ret setObject:items forKey:@"items"];
  5566. sqlite3_finalize(statement);
  5567. } else {
  5568. DebugLog(@"nothing...");
  5569. }
  5570. DebugLog(@"count:%d",count);
  5571. [iSalesDB close_db:db];
  5572. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5573. return ret;
  5574. }
  5575. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5576. {
  5577. return [self categoryList:params limited:YES];
  5578. }
  5579. # pragma mark item search
  5580. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5581. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5582. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5583. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5584. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5585. // category
  5586. NSDictionary *category_menu = [self offline_category_menu];
  5587. [filter setValue:category_menu forKey:@"category"];
  5588. NSString* where= nil;
  5589. NSString* orderby= @"m.name";
  5590. if (!filterSearch) {
  5591. int covertype = [[params valueForKey:@"covertype"] intValue];
  5592. switch (covertype) {
  5593. case 0:
  5594. {
  5595. where=@"m.category like'%%#005#%%'";
  5596. break;
  5597. }
  5598. case 1:
  5599. {
  5600. where=@"m.alert like '%QS%'";
  5601. break;
  5602. }
  5603. case 2:
  5604. {
  5605. where=@"m.availability>0";
  5606. break;
  5607. }
  5608. case 3:
  5609. {
  5610. where=@"m.best_seller>0";
  5611. orderby=@"m.best_seller desc,m.name asc";
  5612. break;
  5613. }
  5614. default:
  5615. where=@"1=1";
  5616. break;
  5617. }
  5618. }
  5619. int limit = [[params valueForKey:@"limit"] intValue];
  5620. int offset = [[params valueForKey:@"offset"] intValue];
  5621. NSString *limit_str = @"";
  5622. if (limited) {
  5623. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5624. }
  5625. sqlite3 *db = [iSalesDB get_db];
  5626. // [iSalesDB AddExFunction:db];
  5627. int count;
  5628. NSString *sqlQuery = nil;
  5629. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5630. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color 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];
  5631. double price_min = 0;
  5632. double price_max = 0;
  5633. if (filterSearch) {
  5634. // alert
  5635. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5636. NSString *alert = params[@"alert"];
  5637. if ([alert isEqualToString:@"Display All"]) {
  5638. alert = [NSString stringWithFormat:@""];
  5639. } else {
  5640. alert = [self translateSingleQuote:alert];
  5641. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5642. }
  5643. // available
  5644. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5645. NSString *available = params[@"available"];
  5646. NSString *available_condition;
  5647. if ([available isEqualToString:@"Display All"]) {
  5648. available_condition = @"";
  5649. } else if ([available isEqualToString:@"Available Now"]) {
  5650. available_condition = @"and availability > 0";
  5651. } else {
  5652. available_condition = @"and availability == 0";
  5653. }
  5654. // best seller
  5655. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5656. NSString *best_seller = @"";
  5657. NSString *order_best_seller = @"m.name asc";
  5658. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5659. best_seller = @"and best_seller > 0";
  5660. order_best_seller = @"m.best_seller desc,m.name asc";
  5661. }
  5662. // price
  5663. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5664. NSString *price = params[@"price"];
  5665. price_min = 0;
  5666. price_max = MAXFLOAT;
  5667. if (appDelegate.user) {
  5668. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5669. NSMutableString *priceName = [NSMutableString string];
  5670. for (int i = 0; i < priceTypeArray.count; i++) {
  5671. NSString *pricetype = priceTypeArray[i];
  5672. pricetype = [self translateSingleQuote:pricetype];
  5673. if (i == 0) {
  5674. [priceName appendFormat:@"'%@'",pricetype];
  5675. } else {
  5676. [priceName appendFormat:@",'%@'",pricetype];
  5677. }
  5678. }
  5679. if ([price isEqualToString:@"Display All"]) {
  5680. price = [NSString stringWithFormat:@""];
  5681. } else if([price containsString:@"+"]){
  5682. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5683. price_min = [price doubleValue];
  5684. 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];
  5685. } else {
  5686. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5687. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5688. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5689. 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];
  5690. }
  5691. } else {
  5692. price = @"";
  5693. }
  5694. // sold_by_qty : Sold in quantities of %@
  5695. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5696. NSString *qty = params[@"sold_by_qty"];
  5697. if ([qty isEqualToString:@"Display All"]) {
  5698. qty = @"";
  5699. } else {
  5700. qty = [self translateSingleQuote:qty];
  5701. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5702. }
  5703. // category
  5704. NSString *category_id = params[@"ctgId"];
  5705. NSMutableArray *cate_id_array = nil;
  5706. NSMutableString *cateWhere = [NSMutableString string];
  5707. if ([category_id isEqualToString:@""] || !category_id) {
  5708. [cateWhere appendString:@"1 = 1"];
  5709. } else {
  5710. if ([category_id containsString:@","]) {
  5711. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5712. } else {
  5713. cate_id_array = [@[category_id] mutableCopy];
  5714. }
  5715. /*-----------*/
  5716. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5717. [cateWhere appendString:@"("];
  5718. for (int i = 0; i < cate_id_array.count; i++) {
  5719. for (NSString *key0 in cateDic.allKeys) {
  5720. if ([key0 containsString:@"category_"]) {
  5721. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  5722. for (NSString *key1 in category0.allKeys) {
  5723. if ([key1 containsString:@"category_"]) {
  5724. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  5725. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5726. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5727. cate_id_array[i] = [category1 objectForKey:@"id"];
  5728. if (i == 0) {
  5729. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5730. } else {
  5731. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5732. }
  5733. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5734. [category0 setValue:category1 forKey:key1];
  5735. [cateDic setValue:category0 forKey:key0];
  5736. }
  5737. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  5738. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5739. cate_id_array[i] = [category0 objectForKey:@"id"];
  5740. if (i == 0) {
  5741. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5742. } else {
  5743. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5744. }
  5745. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5746. [cateDic setValue:category0 forKey:key0];
  5747. }
  5748. }
  5749. }
  5750. }
  5751. }
  5752. }
  5753. [cateWhere appendString:@")"];
  5754. [filter setValue:cateDic forKey:@"category"];
  5755. }
  5756. // where bestseller > 0 order by bestseller desc
  5757. // sql query: alert availability(int) best_seller(int) price qty
  5758. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from (select name,description,product_id,closeout,best_seller,more_color 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];
  5759. // count
  5760. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5761. }
  5762. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5763. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5764. if (!appDelegate.user) {
  5765. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5766. }
  5767. [ret setValue:filter forKey:@"filter"];
  5768. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5769. sqlite3_stmt * statement;
  5770. [ret setValue:@"2" forKey:@"result"];
  5771. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5772. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5773. // int count=0;
  5774. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5775. {
  5776. int i=0;
  5777. while (sqlite3_step(statement) == SQLITE_ROW)
  5778. {
  5779. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5780. char *name = (char*)sqlite3_column_text(statement, 0);
  5781. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5782. char *description = (char*)sqlite3_column_text(statement, 1);
  5783. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5784. int product_id = sqlite3_column_int(statement, 2);
  5785. int wid = sqlite3_column_int(statement, 3);
  5786. int closeout = sqlite3_column_int(statement, 4);
  5787. int cid = sqlite3_column_int(statement, 5);
  5788. int wisdelete = sqlite3_column_int(statement, 6);
  5789. int more_color = sqlite3_column_int(statement, 7);
  5790. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5791. if(wid !=0 && wisdelete != 1)
  5792. [item setValue:@"true" forKey:@"wish_exists"];
  5793. else
  5794. [item setValue:@"false" forKey:@"wish_exists"];
  5795. if(closeout==0)
  5796. [item setValue:@"false" forKey:@"is_closeout"];
  5797. else
  5798. [item setValue:@"true" forKey:@"is_closeout"];
  5799. if(cid==0)
  5800. [item setValue:@"false" forKey:@"cart_exists"];
  5801. else
  5802. [item setValue:@"true" forKey:@"cart_exists"];
  5803. if (more_color == 0) {
  5804. [item setObject:@(false) forKey:@"more_color"];
  5805. } else if (more_color == 1) {
  5806. [item setObject:@(true) forKey:@"more_color"];
  5807. }
  5808. [item addEntriesFromDictionary:imgjson];
  5809. // [item setValue:nsurl forKey:@"img"];
  5810. [item setValue:nsname forKey:@"fash_name"];
  5811. [item setValue:nsdescription forKey:@"description"];
  5812. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5813. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5814. i++;
  5815. }
  5816. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5817. [ret setObject:items forKey:@"items"];
  5818. sqlite3_finalize(statement);
  5819. }
  5820. [iSalesDB close_db:db];
  5821. DebugLog(@"data string: %@",[RAConvertor dict2string:ret] );
  5822. return ret;
  5823. }
  5824. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5825. {
  5826. return [self itemsearch:params limited:YES];
  5827. }
  5828. #pragma mark order detail
  5829. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5830. if (str1.length == 0) {
  5831. str1 = @"&nbsp";
  5832. }
  5833. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5834. return str;
  5835. }
  5836. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5837. {
  5838. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5839. [fromformatter setDateFormat:from];
  5840. NSDate *date = [fromformatter dateFromString:datetime];
  5841. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5842. [toformatter setDateFormat:to];
  5843. NSString * ret = [toformatter stringFromDate:date];
  5844. return ret;
  5845. }
  5846. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5847. // 把毫秒去掉
  5848. if ([dateTime containsString:@"."]) {
  5849. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5850. }
  5851. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5852. formatter.dateFormat = formate;
  5853. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5854. NSDate *date = [formatter dateFromString:dateTime];
  5855. formatter.dateFormat = newFormate;
  5856. NSString *result = [formatter stringFromDate:date];
  5857. return result ? result : @"";
  5858. }
  5859. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5860. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5861. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5862. }
  5863. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5864. {
  5865. DebugLog(@"offline oderdetail params: %@",params);
  5866. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5867. int orderId = [params[@"orderId"] intValue];
  5868. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5869. // decrypt card number and card security code
  5870. // 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 ];
  5871. 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,c.city,c.state,c.zipcode,c.country,o.must_call 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,must_call from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5872. sqlite3 *db = [iSalesDB get_db];
  5873. sqlite3_stmt * statement;
  5874. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5875. NSString *nssoid = @"";
  5876. NSString* orderinfo = @"";
  5877. NSString *moreInfo = @"";
  5878. double handlingFee = 0;
  5879. double payments_and_credist = 0;
  5880. double totalPrice = 0;
  5881. double shippingFee = 0;
  5882. double lift_gate = 0;
  5883. NSString *customer_contact = @"";
  5884. NSString *customer_email = @"";
  5885. NSString *customer_fax = @"";
  5886. NSString *customer_phone = @"";
  5887. NSString *customer_city = @"";
  5888. NSString *customer_state = @"";
  5889. NSString *customer_zipcode = @"";
  5890. NSString *customer_country = @"";
  5891. BOOL must_call = NO;
  5892. __block NSMutableDictionary *section_0 = [NSMutableDictionary dictionary];
  5893. __block NSMutableDictionary *section_1 = [NSMutableDictionary dictionary];
  5894. __block NSMutableDictionary *section_2 = [NSMutableDictionary dictionary];
  5895. __block NSMutableDictionary *section_3 = [NSMutableDictionary dictionary];
  5896. __block NSMutableDictionary *section_4 = [NSMutableDictionary dictionary];
  5897. __block NSMutableDictionary *price_data = [NSMutableDictionary dictionary];
  5898. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5899. {
  5900. if (sqlite3_step(statement) == SQLITE_ROW)
  5901. {
  5902. int order_id = sqlite3_column_int(statement, 0);
  5903. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5904. // ret[@"sign_url"] = sign_url;
  5905. section_1 = @{
  5906. @"data":sign_url,
  5907. @"title":@"Signature",
  5908. @"type":@"sign_url"
  5909. }.mutableCopy;
  5910. [ret setObject:section_1 forKey:@"section_1"];
  5911. customer_contact = [self textAtColumn:52 statement:statement];
  5912. customer_email = [self textAtColumn:53 statement:statement];
  5913. customer_phone = [self textAtColumn:54 statement:statement];
  5914. customer_fax = [self textAtColumn:55 statement:statement];
  5915. customer_city = [self textAtColumn:60 statement:statement];
  5916. customer_state = [self textAtColumn:61 statement:statement];
  5917. customer_zipcode = [self textAtColumn:62 statement:statement];
  5918. customer_country = [self textAtColumn:63 statement:statement];
  5919. must_call = [[self textAtColumn:64 statement:statement] boolValue];
  5920. int offline_edit=sqlite3_column_int(statement, 56);
  5921. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5922. char *soid = (char*)sqlite3_column_text(statement, 1);
  5923. if(soid==nil)
  5924. soid= "";
  5925. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5926. // so#
  5927. ret[@"so#"] = nssoid;
  5928. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5929. if(poNumber==nil)
  5930. poNumber= "";
  5931. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5932. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5933. if(create_time==nil)
  5934. create_time= "";
  5935. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5936. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5937. int status = sqlite3_column_int(statement, 4);
  5938. int erpStatus = sqlite3_column_int(statement, 49);
  5939. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5940. // status
  5941. if (status > 1 && status != 3) {
  5942. status = erpStatus;
  5943. } else if (status == 3) {
  5944. status = 15;
  5945. }
  5946. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5947. ret[@"order_status"] = nsstatus;
  5948. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5949. if(company_name==nil)
  5950. company_name= "";
  5951. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5952. // company name
  5953. ret[@"company_name"] = nscompany_name;
  5954. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5955. if(customer_contact==nil)
  5956. customer_contact= "";
  5957. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5958. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5959. if(addr_1==nil)
  5960. addr_1="";
  5961. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5962. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5963. if(addr_2==nil)
  5964. addr_2="";
  5965. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5966. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5967. if(addr_3==nil)
  5968. addr_3="";
  5969. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5970. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5971. if(addr_4==nil)
  5972. addr_4="";
  5973. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5974. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5975. [arr_addr addObject:nsaddr_1];
  5976. [arr_addr addObject:nsaddr_2];
  5977. [arr_addr addObject:nsaddr_3];
  5978. [arr_addr addObject:nsaddr_4];
  5979. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  5980. [arr_addr addObject:customer_state];
  5981. [arr_addr addObject:customer_zipcode];
  5982. [arr_addr addObject:customer_country];
  5983. // NSString * customer_address = [RAConvertor arr2string:arr_addr separator:@" " trim:true];
  5984. NSString *customer_address = [NSString stringWithFormat:@"%@<br/>%@, %@ %@ %@",nsaddr_1,customer_city,customer_state,customer_zipcode,customer_country];
  5985. char *logist = (char*)sqlite3_column_text(statement, 11);
  5986. if(logist==nil)
  5987. logist= "";
  5988. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5989. if (/*status == -11 || */status == 10 || status == 11) {
  5990. nslogist = [self textAtColumn:59 statement:statement];
  5991. };
  5992. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5993. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5994. shipping = @"Shipping To Be Quoted";
  5995. } else {
  5996. shippingFee = sqlite3_column_double(statement, 12);
  5997. }
  5998. // Shipping
  5999. // ret[@"Shipping"] = shipping;
  6000. NSDictionary *shipping_item = @{
  6001. @"title":@"Shipping",
  6002. @"value":shipping
  6003. };
  6004. [price_data setObject:shipping_item forKey:@"item_1"];
  6005. int have_lift_gate = sqlite3_column_int(statement, 17);
  6006. lift_gate = sqlite3_column_double(statement, 13);
  6007. // Liftgate Fee(No loading dock)
  6008. if (!have_lift_gate) {
  6009. lift_gate = 0;
  6010. }
  6011. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6012. NSString *liftgate_value = [NSString stringWithFormat:@"$%.2f",lift_gate];
  6013. if (sqlite3_column_int(statement, 57)) {
  6014. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  6015. liftgate_value = @"Shipping To Be Quoted";
  6016. }
  6017. NSDictionary *liftgate_item = @{
  6018. @"title":@"Liftgate Fee(No loading dock)",
  6019. @"value":liftgate_value
  6020. };
  6021. [price_data setObject:liftgate_item forKey:@"item_2"];
  6022. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  6023. // [ret removeObjectForKey:@"Shipping"];
  6024. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6025. // }
  6026. //
  6027. // if (have_lift_gate) {
  6028. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  6029. // }
  6030. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  6031. if(general_notes==nil)
  6032. general_notes= "";
  6033. NSString *nsgeneral_notes= [NSString stringWithFormat:@"MUST MAKE APPOINTMENT BEFORE DELIVERY = %@;<br/>%@",must_call ? @"YES" : @"NO",[[NSString alloc]initWithUTF8String:general_notes]];
  6034. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  6035. if(internal_notes==nil)
  6036. internal_notes= "";
  6037. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  6038. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  6039. if(payment_type==nil)
  6040. payment_type= "";
  6041. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  6042. // order info
  6043. orderinfo = [self textFileName:@"order_info.html"];
  6044. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  6045. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  6046. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  6047. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  6048. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  6049. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  6050. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  6051. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  6052. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  6053. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  6054. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  6055. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  6056. NSString *payment = nil;
  6057. // id -> show
  6058. __block NSString *show_pay_type = nspayment_type;
  6059. [iSalesDB jk_query:[NSString stringWithFormat:@"select pay_type from payment_type where type_id = '%@';",nspayment_type] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6060. char *show_typ_ch = (char*)sqlite3_column_text(stmt, 0);
  6061. if (show_typ_ch != NULL) {
  6062. show_pay_type = [NSString stringWithUTF8String:show_typ_ch];
  6063. }
  6064. }];
  6065. if([nspayment_type isEqualToString:@"Credit Card"] || [nspayment_type isEqualToString:@"Visa/Master"])
  6066. {
  6067. payment = [self textFileName:@"creditcardpayment.html"];
  6068. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  6069. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  6070. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  6071. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  6072. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  6073. NSString *card_type = [self textAtColumn:42 statement:statement];
  6074. if (card_type.length > 0) { // 显示星号
  6075. card_type = @"****";
  6076. }
  6077. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  6078. if (card_number.length > 0 && card_number.length > 4) {
  6079. for (int i = 0; i < card_number.length - 4; i++) {
  6080. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  6081. }
  6082. } else {
  6083. card_number = @"";
  6084. }
  6085. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6086. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6087. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6088. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6089. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6090. card_expiration = @"****";
  6091. }
  6092. NSString *card_city = [self textAtColumn:46 statement:statement];
  6093. NSString *card_state = [self textAtColumn:47 statement:statement];
  6094. payment = [self replaceHtml:payment String:@"Payment_Type" withString:show_pay_type];
  6095. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6096. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6097. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6098. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6099. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6100. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6101. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6102. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6103. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6104. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6105. }
  6106. else
  6107. {
  6108. payment=[self textFileName:@"normalpayment.html"];
  6109. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6110. }
  6111. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6112. ret[@"result"]= [NSNumber numberWithInt:2];
  6113. // more info
  6114. moreInfo = [self textFileName:@"more_info.html"];
  6115. /*****ship to******/
  6116. // ShipToCompany_or_&nbsp
  6117. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6118. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6119. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6120. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6121. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6122. shipToAddr = [shipToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6123. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6124. /*****ship from******/
  6125. // ShipFromCompany_or_&nbsp
  6126. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6127. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6128. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6129. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6130. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6131. shipFromAddr = [shipFromAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6132. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6133. /*****freight to******/
  6134. // FreightBillToCompany_or_&nbsp
  6135. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6136. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6137. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6138. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6139. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6140. freightBillToAddr = [freightBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6141. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6142. /*****merchandise to******/
  6143. // MerchandiseBillToCompany_or_&nbsp
  6144. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6145. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6146. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6147. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6148. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6149. merchandiseBillToAddr = [merchandiseBillToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6150. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6151. /*****return to******/
  6152. // ReturnToCompany_or_&nbsp
  6153. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6154. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6155. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6156. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6157. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6158. returnToAddr = [returnToAddr stringByReplacingOccurrencesOfString:@"\r\n" withString:@"<br/>"];
  6159. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6160. //
  6161. // DebugLog(@"more info : %@",moreInfo);
  6162. // handling fee
  6163. handlingFee = sqlite3_column_double(statement, 33);
  6164. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6165. NSString *handling_fee_value = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6166. if (sqlite3_column_int(statement, 58)) {
  6167. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6168. handling_fee_value = @"Shipping To Be Quoted";
  6169. }
  6170. NSDictionary *handling_fee_item = @{
  6171. @"title":@"Handling Fee",
  6172. @"value":handling_fee_value
  6173. };
  6174. [price_data setObject:handling_fee_item forKey:@"item_3"];
  6175. //
  6176. // customer info
  6177. customerID = [self textAtColumn:36 statement:statement];
  6178. // mode
  6179. ret[@"mode"] = appDelegate.mode;
  6180. // model_count
  6181. ret[@"model_count"] = @(0);
  6182. }
  6183. sqlite3_finalize(statement);
  6184. }
  6185. [iSalesDB close_db:db];
  6186. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6187. // "customer_email" = "Shui Hu";
  6188. // "customer_fax" = "";
  6189. // "customer_first_name" = F;
  6190. // "customer_last_name" = L;
  6191. // "customer_name" = ",da He Xiang Dong Liu A";
  6192. // "customer_phone" = "Hey Xuan Feng";
  6193. contactInfo[@"customer_phone"] = customer_phone;
  6194. contactInfo[@"customer_fax"] = customer_fax;
  6195. contactInfo[@"customer_email"] = customer_email;
  6196. NSString *first_name = @"";
  6197. NSString *last_name = @"";
  6198. if ([customer_contact isEqualToString:@""]) {
  6199. } else if ([customer_contact containsString:@" "]) {
  6200. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6201. first_name = [customer_contact substringToIndex:first_space_index];
  6202. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6203. }
  6204. contactInfo[@"customer_first_name"] = first_name;
  6205. contactInfo[@"customer_last_name"] = last_name;
  6206. ret[@"customerInfo"] = contactInfo;
  6207. // models
  6208. if (nssoid) {
  6209. __block NSMutableDictionary *model_data = [NSMutableDictionary dictionary];
  6210. __block double TotalCuft = 0;
  6211. __block double TotalWeight = 0;
  6212. __block int TotalCarton = 0;
  6213. __block double allItemPrice = 0;
  6214. 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];
  6215. sqlite3 *db1 = [iSalesDB get_db];
  6216. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6217. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6218. int product_id = sqlite3_column_int(stmt, 0);
  6219. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6220. int item_id = sqlite3_column_int(stmt, 7);
  6221. NSString* Price=nil;
  6222. if(str_price==nil)
  6223. {
  6224. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6225. if(price==nil)
  6226. Price=@"No Price.";
  6227. else
  6228. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6229. }
  6230. else
  6231. {
  6232. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6233. }
  6234. double discount = sqlite3_column_double(stmt, 2);
  6235. int item_count = sqlite3_column_int(stmt, 3);
  6236. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6237. NSString *nsline_note=nil;
  6238. if(line_note!=nil)
  6239. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6240. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6241. // NSString *nsname = nil;
  6242. // if(name!=nil)
  6243. // nsname= [[NSString alloc]initWithUTF8String:name];
  6244. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6245. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6246. // NSString *nsdescription=nil;
  6247. // if(description!=nil)
  6248. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6249. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6250. // int stockUom = sqlite3_column_int(stmt, 8);
  6251. // int _id = sqlite3_column_int(stmt, 9);
  6252. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6253. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6254. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6255. int carton=[bsubtotaljson[@"carton"] intValue];
  6256. TotalCuft += cuft;
  6257. TotalWeight += weight;
  6258. TotalCarton += carton;
  6259. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6260. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6261. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6262. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6263. itemjson[@"note"]=nsline_note;
  6264. itemjson[@"origin_price"] = Price;
  6265. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6266. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6267. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6268. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6269. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6270. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6271. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6272. if(itemjson[@"combine"] != nil)
  6273. {
  6274. // int citem=0;
  6275. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6276. for(int bc=0;bc<bcount;bc++)
  6277. {
  6278. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6279. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6280. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6281. subTotal += uprice * modulus * item_count;
  6282. }
  6283. }
  6284. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6285. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6286. // ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6287. // ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6288. itemjson[@"type"] = @"order_item";
  6289. model_data[[NSString stringWithFormat:@"item_%ld",(*count)++]] = itemjson;
  6290. model_data[@"count"] = [NSNumber numberWithInteger:*count];
  6291. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6292. allItemPrice += subTotal;
  6293. }];
  6294. section_3[@"data"] = model_data;
  6295. section_3[@"type"] = @"sub_order";
  6296. section_3[@"title"] = @"Models";
  6297. section_3[@"switch"] = @(false);
  6298. ret[@"section_3"] = section_3;
  6299. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6300. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6301. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6302. // payments/Credits
  6303. // payments_and_credist = sqlite3_column_double(statement, 34);
  6304. payments_and_credist = allItemPrice;
  6305. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6306. NSDictionary *sub_total_item = @{
  6307. @"title":@"Sub-Total",
  6308. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6309. };
  6310. [price_data setObject:sub_total_item forKey:@"item_0"];
  6311. // // total
  6312. // totalPrice = sqlite3_column_double(statement, 35);
  6313. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6314. } else {
  6315. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6316. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6317. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6318. // payments/Credits
  6319. payments_and_credist = 0;
  6320. // ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6321. NSDictionary *sub_total_item = @{
  6322. @"title":@"Sub-Total",
  6323. @"value":[NSString stringWithFormat:@"$%.2f",payments_and_credist]
  6324. };
  6325. [price_data setObject:sub_total_item forKey:@"item_0"];
  6326. }
  6327. // total
  6328. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6329. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6330. NSDictionary *total_item = @{
  6331. @"title":@"Total",
  6332. @"value":[NSString stringWithFormat:@"$%.2f",totalPrice]
  6333. };
  6334. [price_data setObject:total_item forKey:@"item_4"];
  6335. [price_data setObject:@(price_data.allKeys.count) forKey:@"count"];
  6336. section_4 = @{
  6337. @"data":price_data,
  6338. @"title":@"Price Info",
  6339. @"type":@"price_info"
  6340. }.mutableCopy;
  6341. ret[@"section_4"] = section_4;
  6342. // ret[@"order_info"]= orderinfo;
  6343. section_0 = @{
  6344. @"data":orderinfo,
  6345. @"title":@"Order Info",
  6346. @"type":@"order_info"
  6347. }.mutableCopy;
  6348. ret[@"section_0"] = section_0;
  6349. // ret[@"more_order_info"] = moreInfo;
  6350. section_2 = @{
  6351. @"data":moreInfo,
  6352. @"title":@"More Info",
  6353. @"type":@"more_order_info"
  6354. }.mutableCopy;
  6355. ret[@"section_2"] = section_2;
  6356. ret[@"count"] = @(5);
  6357. return [RAConvertor dict2data:ret];
  6358. }
  6359. #pragma mark order list
  6360. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6361. NSString *nsstatus = @"";
  6362. switch (status) {
  6363. case 0:
  6364. {
  6365. nsstatus=@"Temp Order";
  6366. break;
  6367. }
  6368. case 1:
  6369. {
  6370. nsstatus=@"Saved Order";
  6371. break;
  6372. }
  6373. case 2: {
  6374. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6375. break;
  6376. }
  6377. case 3:
  6378. case 15:
  6379. {
  6380. nsstatus=@"Cancelled";
  6381. break;
  6382. }
  6383. case 10:
  6384. {
  6385. nsstatus=@"Quote Submitted";
  6386. break;
  6387. }
  6388. case 11:
  6389. {
  6390. nsstatus=@"Sales Order Submitted";
  6391. break;
  6392. }
  6393. case 12:
  6394. {
  6395. nsstatus=@"Processing";
  6396. break;
  6397. }
  6398. case 13:
  6399. {
  6400. nsstatus=@"Shipped";
  6401. break;
  6402. }
  6403. case 14:
  6404. {
  6405. nsstatus=@"Closed";
  6406. break;
  6407. }
  6408. case -11:
  6409. {
  6410. nsstatus = @"Ready For Submit";
  6411. break;
  6412. }
  6413. default:
  6414. break;
  6415. }
  6416. return nsstatus;
  6417. }
  6418. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6419. double total = 0;
  6420. __block double payments_and_credist = 0;
  6421. __block double allItemPrice = 0;
  6422. // sqlite3 *db1 = [iSalesDB get_db];
  6423. if (so_id) {
  6424. // 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];
  6425. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id,customer_cid from (select c.product_id,decrypt(c.str_price) as price,c.discount,c.item_count,c.item_id,o.customer_cid from offline_cart as c left join offline_order as o on c.so_no = o.so_id where c.so_no='%@');",so_id];
  6426. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6427. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6428. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6429. int product_id = sqlite3_column_int(stmt, 0);
  6430. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6431. int discount = sqlite3_column_int(stmt, 2);
  6432. int item_count = sqlite3_column_int(stmt, 3);
  6433. // int item_id = sqlite3_column_int(stmt, 7);
  6434. int item_id = sqlite3_column_int(stmt, 4);
  6435. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6436. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6437. NSString* Price=nil;
  6438. if(str_price==nil)
  6439. {
  6440. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6441. if(price==nil)
  6442. Price=@"No Price.";
  6443. else
  6444. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6445. }
  6446. else
  6447. {
  6448. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6449. }
  6450. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6451. itemjson[@"The unit price"]=Price;
  6452. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6453. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6454. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6455. if(itemjson[@"combine"] != nil)
  6456. {
  6457. // int citem=0;
  6458. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6459. for(int bc=0;bc<bcount;bc++)
  6460. {
  6461. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6462. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6463. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6464. subTotal += uprice * modulus * item_count;
  6465. }
  6466. }
  6467. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6468. allItemPrice += subTotal;
  6469. }];
  6470. payments_and_credist = allItemPrice;
  6471. } else {
  6472. // payments/Credits
  6473. payments_and_credist = 0;
  6474. }
  6475. // lift_gate handlingFee shippingFee
  6476. __block double lift_gate = 0;
  6477. __block double handlingFee = 0;
  6478. __block double shippingFee = 0;
  6479. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6480. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6481. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6482. if (have_lift_gate) {
  6483. lift_gate = sqlite3_column_double(stmt, 1);
  6484. }
  6485. handlingFee = sqlite3_column_double(stmt, 2);
  6486. shippingFee = sqlite3_column_double(stmt, 3);
  6487. }];
  6488. // total
  6489. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6490. if (close) {
  6491. [iSalesDB close_db:db1];
  6492. }
  6493. return total;
  6494. }
  6495. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6496. {
  6497. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6498. int limit = [[params valueForKey:@"limit"] intValue];
  6499. int offset = [[params valueForKey:@"offset"] intValue];
  6500. NSString* keyword = [params valueForKey:@"keyWord"];
  6501. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6502. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6503. NSString* where=@"1 = 1";
  6504. if(keyword.length>0)
  6505. 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]];
  6506. if (orderStatus.length > 0) {
  6507. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6508. if (order_status_array.count == 1) {
  6509. int status_value = [[order_status_array firstObject] integerValue];
  6510. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6511. if (status_value == 15 || status_value == 3) {
  6512. where = [where stringByAppendingString:[NSString stringWithFormat:@" and (o.status = 3 or o.erpOrderStatus = 15)"]];
  6513. } else {
  6514. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6515. }
  6516. } else {
  6517. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6518. }
  6519. } else if (order_status_array.count > 1) {
  6520. for (int i = 0; i < order_status_array.count;i++) {
  6521. NSString *status = order_status_array[i];
  6522. NSString *condition = @" or";
  6523. if (i == 0) {
  6524. condition = @" and (";
  6525. }
  6526. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6527. int status_value = [status integerValue];
  6528. if (status_value <= 1 || status_value == 3 || status_value == 15) {
  6529. if (status_value == 15 || status_value == 3) {
  6530. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ (o.status = 3 or o.erpOrderStatus = 15)",condition]];
  6531. } else {
  6532. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6533. }
  6534. } else {
  6535. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6536. }
  6537. }
  6538. where = [where stringByAppendingString:@" )"];
  6539. }
  6540. }
  6541. 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.create_time desc limit %d offset %d" ,where, limit, offset];
  6542. // DebugLog(@"order list sql: %@",sqlQuery);
  6543. sqlite3 *db = [iSalesDB get_db];
  6544. sqlite3_stmt * statement;
  6545. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6546. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6547. {
  6548. int count=0;
  6549. while (sqlite3_step(statement) == SQLITE_ROW)
  6550. {
  6551. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6552. int order_id = sqlite3_column_double(statement, 0);
  6553. char *soid = (char*)sqlite3_column_text(statement, 1);
  6554. if(soid==nil)
  6555. soid= "";
  6556. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6557. int status = sqlite3_column_double(statement, 2);
  6558. int erpStatus = sqlite3_column_double(statement, 9);
  6559. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6560. if(sales_rep==nil)
  6561. sales_rep= "";
  6562. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6563. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6564. if(create_by==nil)
  6565. create_by= "";
  6566. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6567. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6568. if(company_name==nil)
  6569. company_name= "";
  6570. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6571. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6572. if(create_time==nil)
  6573. create_time= "";
  6574. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6575. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6576. // double total_price = sqlite3_column_double(statement, 7);
  6577. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6578. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6579. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6580. int offline_edit = sqlite3_column_int(statement, 10);
  6581. // ": "JH",
  6582. // "": "$8307.00",
  6583. // "": "MOB1608050001",
  6584. // "": "ArpithaT",
  6585. // "": "1st Stage Property Transformations",
  6586. // "": "JANICE SUTTON",
  6587. // "": 2255,
  6588. // "": "08/02/2016 09:49:18",
  6589. // "": 1,
  6590. // "": "Saved Order"
  6591. // "": "1470384050483",
  6592. // "model_count": "6 / 28"
  6593. item[@"sales_rep"]= nssales_rep;
  6594. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6595. item[@"so#"]= nssoid;
  6596. item[@"create_by"]= nscreate_by;
  6597. item[@"customer_name"]= nscompany_name;
  6598. item[@"customer_contact"] = customer_contact;
  6599. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6600. item[@"purchase_time"]= nscreate_time;
  6601. int statusCode = status;
  6602. if (statusCode == 2) {
  6603. statusCode = erpStatus;
  6604. } else if (statusCode == 3) {
  6605. statusCode = 15;
  6606. }
  6607. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6608. item[@"order_status"]= nsstatus;
  6609. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6610. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6611. // item[@"model_count"]
  6612. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6613. count++;
  6614. }
  6615. ret[@"count"]= [NSNumber numberWithInt:count];
  6616. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6617. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6618. ret[@"result"]= [NSNumber numberWithInt:2];
  6619. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6620. ret[@"time_zone"] = @"PST";
  6621. sqlite3_finalize(statement);
  6622. }
  6623. 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];
  6624. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6625. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6626. [iSalesDB close_db:db];
  6627. return [RAConvertor dict2data:ret];
  6628. }
  6629. #pragma mark update gnotes
  6630. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6631. {
  6632. DebugLog(@"params: %@",params);
  6633. // comments = Meyoyoyoyoyoyoy;
  6634. // orderCode = MOB1608110001;
  6635. // password = 123456;
  6636. // user = EvanK;
  6637. 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]];
  6638. int ret = [iSalesDB execSql:sql];
  6639. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6640. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6641. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6642. // [dic setValue:@"160409" forKey:@"min_ver"];
  6643. return [RAConvertor dict2data:dic];
  6644. }
  6645. #pragma mark move to wishlist
  6646. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6647. {
  6648. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6649. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6650. _id = [NSString stringWithFormat:@"(%@)",_id];
  6651. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6652. sqlite3 *db = [iSalesDB get_db];
  6653. __block NSString *product_id = @"";
  6654. __block NSString *item_count_str = @"";
  6655. // __block NSString *item_id = nil;
  6656. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6657. // product_id = [self textAtColumn:0 statement:stmt];
  6658. int item_count = sqlite3_column_int(stmt, 1);
  6659. // item_id = [self textAtColumn:2 statement:stmt];
  6660. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6661. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6662. if (p_id.length) {
  6663. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6664. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6665. }
  6666. }];
  6667. [iSalesDB close_db:db];
  6668. // 去除第一个,
  6669. if (product_id.length > 1) {
  6670. product_id = [product_id substringFromIndex:1];
  6671. }
  6672. if (item_count_str.length > 1) {
  6673. item_count_str = [item_count_str substringFromIndex:1];
  6674. }
  6675. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6676. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6677. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6678. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6679. sqlite3 *db1 = [iSalesDB get_db];
  6680. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6681. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6682. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6683. // 删除
  6684. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6685. int ret = [iSalesDB execSql:deleteSql db:db1];
  6686. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6687. [iSalesDB close_db:db1];
  6688. return [RAConvertor dict2data:dic];
  6689. }
  6690. #pragma mark cart delete
  6691. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6692. {
  6693. // cartItemId = 548;
  6694. // orderCode = MOB1608110001;
  6695. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6696. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6697. _id = [NSString stringWithFormat:@"(%@)",_id];
  6698. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6699. int ret = [iSalesDB execSql:sql];
  6700. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6701. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6702. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6703. // [dic setValue:@"160409" forKey:@"min_ver"];
  6704. return [RAConvertor dict2data:dic];
  6705. }
  6706. #pragma mark set price
  6707. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6708. {
  6709. DebugLog(@"cart set price params: %@",params);
  6710. // "cartitem_id" = 1;
  6711. // discount = "0.000000";
  6712. // "item_note" = "";
  6713. // price = "269.000000";
  6714. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6715. NSString *notes = [self valueInParams:params key:@"item_note"];
  6716. NSString *discount = [self valueInParams:params key:@"discount"];
  6717. NSString *price = [self valueInParams:params key:@"price"];
  6718. // bool badd_price_changed=false;
  6719. // sqlite3* db=[iSalesDB get_db];
  6720. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6721. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6722. // NSRange range;
  6723. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6724. //
  6725. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6726. // badd_price_changed=true;
  6727. // [iSalesDB close_db:db];
  6728. //
  6729. // if(badd_price_changed)
  6730. // {
  6731. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6732. // }
  6733. //
  6734. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6735. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6736. int ret = [iSalesDB execSql:sql];
  6737. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6738. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6739. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6740. // [dic setValue:@"160409" forKey:@"min_ver"];
  6741. return [RAConvertor dict2data:dic];
  6742. }
  6743. #pragma mark set line notes
  6744. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6745. {
  6746. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6747. NSString *notes = [self valueInParams:params key:@"notes"];
  6748. notes = [self translateSingleQuote:notes];
  6749. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6750. int ret = [iSalesDB execSql:sql];
  6751. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6752. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6753. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6754. // [dic setValue:@"160409" forKey:@"min_ver"];
  6755. return [RAConvertor dict2data:dic];
  6756. }
  6757. #pragma mark set qty
  6758. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6759. {
  6760. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6761. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6762. int item_count = [params[@"inputInt"] intValue];
  6763. // 购买检查数量大于库存
  6764. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6765. if (!appDelegate.can_create_backorder) {
  6766. NSString *checkSql = [NSString stringWithFormat:@"select p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %@;",_id];
  6767. __block BOOL out_of_stock = NO;
  6768. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6769. int availability = sqlite3_column_int(stmt, 0);
  6770. if (availability < item_count) {
  6771. out_of_stock = YES;
  6772. }
  6773. }];
  6774. if (out_of_stock) { // 缺货
  6775. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  6776. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  6777. return [RAConvertor dict2data:dic];
  6778. }
  6779. }
  6780. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6781. int ret = [iSalesDB execSql:sql];
  6782. __block int item_id = 0;
  6783. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6784. item_id = sqlite3_column_int(stmt, 0);
  6785. }];
  6786. sqlite3 *db = [iSalesDB get_db];
  6787. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6788. [iSalesDB close_db:db];
  6789. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6790. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6791. // [dic setValue:@"160409" forKey:@"min_ver"];
  6792. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6793. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6794. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6795. return [RAConvertor dict2data:dic];
  6796. }
  6797. #pragma mark place order
  6798. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6799. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6800. if (aname) {
  6801. [dic setValue:aname forKey:@"aname"];
  6802. }
  6803. if (control) {
  6804. [dic setValue:control forKey:@"control"];
  6805. }
  6806. if (keyboard) {
  6807. [dic setValue:keyboard forKey:@"keyboard"];
  6808. }
  6809. if (name) {
  6810. [dic setValue:name forKey:@"name"];
  6811. }
  6812. if (value) {
  6813. [dic setValue:value forKey:@"value"];
  6814. }
  6815. return dic;
  6816. }
  6817. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6818. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6819. orderCode = [self translateSingleQuote:orderCode];
  6820. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6821. 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];
  6822. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6823. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6824. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6825. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6826. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6827. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6828. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6829. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6830. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6831. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6832. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6833. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6834. NSString *email = [self textAtColumn:11 statement:stmt];
  6835. NSString *phone = [self textAtColumn:12 statement:stmt];
  6836. NSString *fax = [self textAtColumn:13 statement:stmt];
  6837. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6838. NSString *city = [self textAtColumn:15 statement:stmt];
  6839. NSString *state = [self textAtColumn:16 statement:stmt];
  6840. NSString *country = [self textAtColumn:17 statement:stmt];
  6841. NSString *name = [self textAtColumn:18 statement:stmt];
  6842. // contact id
  6843. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6844. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6845. [contact_id_dic setValue:@"text" forKey:@"control"];
  6846. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6847. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6848. [contact_id_dic setValue:@"true" forKey:@"required"];
  6849. [contact_id_dic setValue:contact_id forKey:@"value"];
  6850. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6851. // business card
  6852. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6853. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6854. [business_card_dic setValue:@"img" forKey:@"control"];
  6855. [business_card_dic setValue:@"1" forKey:@"disable"];
  6856. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6857. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6858. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6859. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6860. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6861. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6862. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6863. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6864. // fax
  6865. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6866. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6867. // zipcode
  6868. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6869. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6870. // city
  6871. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6872. [customer_dic setValue:city_dic forKey:@"item_12"];
  6873. // state
  6874. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6875. [customer_dic setValue:state_dic forKey:@"item_13"];
  6876. // country
  6877. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6878. [customer_dic setValue:country_dic forKey:@"item_14"];
  6879. // company name
  6880. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6881. [customer_dic setValue:company_dic forKey:@"item_2"];
  6882. // addr_1
  6883. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6884. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6885. // addr_2
  6886. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6887. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6888. // addr_3
  6889. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6890. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6891. // addr_4
  6892. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6893. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6894. // Contact
  6895. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6896. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6897. // email
  6898. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6899. [customer_dic setValue:email_dic forKey:@"item_8"];
  6900. // phone
  6901. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6902. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6903. // title
  6904. [customer_dic setValue:@"Customer" forKey:@"title"];
  6905. // count
  6906. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6907. }];
  6908. // setting
  6909. NSDictionary *setting = params[@"setting"];
  6910. NSNumber *hide = setting[@"CustomerHide"];
  6911. [customer_dic setValue:hide forKey:@"hide"];
  6912. return customer_dic;
  6913. }
  6914. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6915. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6916. // setting
  6917. NSDictionary *setting = params[@"setting"];
  6918. NSNumber *hide = setting[@"ShipToHide"];
  6919. [dic setValue:hide forKey:@"hide"];
  6920. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6921. orderCode = [self translateSingleQuote:orderCode];
  6922. 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];
  6923. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6924. NSString *cid = [self textAtColumn:0 statement:stmt];
  6925. NSString *name = [self textAtColumn:1 statement:stmt];
  6926. NSString *ext = [self textAtColumn:2 statement:stmt];
  6927. NSString *contact = [self textAtColumn:3 statement:stmt];
  6928. NSString *email = [self textAtColumn:4 statement:stmt];
  6929. NSString *fax = [self textAtColumn:5 statement:stmt];
  6930. NSString *phone = [self textAtColumn:6 statement:stmt];
  6931. // count
  6932. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6933. // title
  6934. [dic setValue:@"Ship To" forKey:@"title"];
  6935. // choose
  6936. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6937. [choose_dic setValue:@"choose" forKey:@"aname"];
  6938. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6939. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6940. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6941. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6942. @"receive_contact" : @"customer_contact",
  6943. @"receive_email" : @"customer_email",
  6944. @"receive_ext" : @"customer_contact_ext",
  6945. @"receive_fax" : @"customer_fax",
  6946. @"receive_name" : @"customer_name",
  6947. @"receive_phone" : @"customer_phone"},
  6948. @"refresh" : [NSNumber numberWithInteger:1],
  6949. @"type" : @"pull"};
  6950. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6951. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6952. @"name" : @"Add new address",
  6953. @"refresh" : [NSNumber numberWithInteger:1],
  6954. @"value" : @"new_addr"
  6955. };
  6956. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6957. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6958. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6959. @"receive_contact" : @"customer_contact",
  6960. @"receive_email" : @"customer_email",
  6961. @"receive_ext" : @"customer_contact_ext",
  6962. @"receive_fax" : @"customer_fax",
  6963. @"receive_name" : @"customer_name",
  6964. @"receive_phone" : @"customer_phone"},
  6965. @"name" : @"select_ship_to",
  6966. @"refresh" : [NSNumber numberWithInteger:1],
  6967. @"value" : @"Sales_Order_Ship_To"};
  6968. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6969. [dic setValue:choose_dic forKey:@"item_0"];
  6970. // contact id
  6971. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6972. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6973. [contact_id_dic setValue:@"true" forKey:@"required"];
  6974. [dic setValue:contact_id_dic forKey:@"item_1"];
  6975. // company name
  6976. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6977. [dic setValue:company_name_dic forKey:@"item_2"];
  6978. // address
  6979. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6980. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6981. if ([required integerValue]) {
  6982. [ext_dic setValue:@"true" forKey:@"required"];
  6983. }
  6984. [dic setValue:ext_dic forKey:@"item_3"];
  6985. // contact
  6986. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6987. [dic setValue:contact_dic forKey:@"item_4"];
  6988. // phone
  6989. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6990. [dic setValue:phone_dic forKey:@"item_5"];
  6991. // fax
  6992. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6993. [dic setValue:fax_dic forKey:@"item_6"];
  6994. // email
  6995. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6996. [dic setValue:email_dic forKey:@"item_7"];
  6997. }];
  6998. return dic;
  6999. }
  7000. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  7001. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7002. // setting
  7003. NSDictionary *setting = params[@"setting"];
  7004. NSNumber *hide = setting[@"ShipFromHide"];
  7005. [dic setValue:hide forKey:@"hide"];
  7006. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7007. orderCode = [self translateSingleQuote:orderCode];
  7008. 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];
  7009. 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';";
  7010. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7011. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  7012. __block NSString *name = [self textAtColumn:1 statement:stmt];
  7013. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  7014. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  7015. __block NSString *email = [self textAtColumn:4 statement:stmt];
  7016. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  7017. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  7018. if (!cid.length) {
  7019. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  7020. cid = [self textAtColumn:0 statement:statment];
  7021. name = [self textAtColumn:1 statement:statment];
  7022. ext = [self textAtColumn:2 statement:statment];
  7023. contact = [self textAtColumn:3 statement:statment];
  7024. email = [self textAtColumn:4 statement:statment];
  7025. fax = [self textAtColumn:5 statement:statment];
  7026. phone = [self textAtColumn:6 statement:statment];
  7027. }];
  7028. }
  7029. // count
  7030. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7031. // title
  7032. [dic setValue:@"Ship From" forKey:@"title"];
  7033. // hide
  7034. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7035. // choose
  7036. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7037. [choose_dic setValue:@"choose" forKey:@"aname"];
  7038. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7039. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7040. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  7041. @"key_map" : @{@"sender_cid" : @"customer_cid",
  7042. @"sender_contact" : @"customer_contact",
  7043. @"sender_email" : @"customer_email",
  7044. @"sender_ext" : @"customer_contact_ext",
  7045. @"sender_fax" : @"customer_fax",
  7046. @"sender_name" : @"customer_name",
  7047. @"sender_phone" : @"customer_phone"},
  7048. @"name" : @"select_cid",
  7049. @"refresh" : [NSNumber numberWithInteger:0],
  7050. @"value" : @"Sales_Order_Ship_From"};
  7051. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  7052. [dic setValue:choose_dic forKey:@"item_0"];
  7053. // contact id
  7054. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7055. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7056. [contact_id_dic setValue:@"true" forKey:@"required"];
  7057. [dic setValue:contact_id_dic forKey:@"item_1"];
  7058. // company name
  7059. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7060. [dic setValue:company_name_dic forKey:@"item_2"];
  7061. // address
  7062. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7063. [dic setValue:ext_dic forKey:@"item_3"];
  7064. // contact
  7065. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7066. [dic setValue:contact_dic forKey:@"item_4"];
  7067. // phone
  7068. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7069. [dic setValue:phone_dic forKey:@"item_5"];
  7070. // fax
  7071. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7072. [dic setValue:fax_dic forKey:@"item_6"];
  7073. // email
  7074. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7075. [dic setValue:email_dic forKey:@"item_7"];
  7076. }];
  7077. return dic;
  7078. }
  7079. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  7080. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7081. // setting
  7082. NSDictionary *setting = params[@"setting"];
  7083. NSNumber *hide = setting[@"FreightBillToHide"];
  7084. [dic setValue:hide forKey:@"hide"];
  7085. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7086. orderCode = [self translateSingleQuote:orderCode];
  7087. 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];
  7088. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7089. NSString *cid = [self textAtColumn:0 statement:stmt];
  7090. NSString *name = [self textAtColumn:1 statement:stmt];
  7091. NSString *ext = [self textAtColumn:2 statement:stmt];
  7092. NSString *contact = [self textAtColumn:3 statement:stmt];
  7093. NSString *email = [self textAtColumn:4 statement:stmt];
  7094. NSString *fax = [self textAtColumn:5 statement:stmt];
  7095. NSString *phone = [self textAtColumn:6 statement:stmt];
  7096. // count
  7097. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7098. // title
  7099. [dic setValue:@"Freight Bill To" forKey:@"title"];
  7100. // hide
  7101. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7102. // choose
  7103. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7104. [choose_dic setValue:@"choose" forKey:@"aname"];
  7105. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7106. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7107. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7108. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  7109. @"shipping_billto_contact" : @"receive_contact",
  7110. @"shipping_billto_email" : @"receive_email",
  7111. @"shipping_billto_ext" : @"receive_ext",
  7112. @"shipping_billto_fax" : @"receive_fax",
  7113. @"shipping_billto_name" : @"receive_name",
  7114. @"shipping_billto_phone" : @"receive_phone"},
  7115. @"type" : @"pull"};
  7116. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7117. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7118. @"type" : @"pull",
  7119. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  7120. @"shipping_billto_contact" : @"customer_contact",
  7121. @"shipping_billto_email" : @"customer_email",
  7122. @"shipping_billto_ext" : @"customer_contact_ext",
  7123. @"shipping_billto_fax" : @"customer_fax",
  7124. @"shipping_billto_name" : @"customer_name",
  7125. @"shipping_billto_phone" : @"customer_phone"}
  7126. };
  7127. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7128. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7129. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  7130. @"shipping_billto_contact" : @"sender_contact",
  7131. @"shipping_billto_email" : @"sender_email",
  7132. @"shipping_billto_ext" : @"sender_ext",
  7133. @"shipping_billto_fax" : @"sender_fax",
  7134. @"shipping_billto_name" : @"sender_name",
  7135. @"shipping_billto_phone" : @"sender_phone"},
  7136. @"type" : @"pull"
  7137. };
  7138. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  7139. NSDictionary *select_freight_bill_to_dic = @{
  7140. @"aname" : @"Select freight bill to",
  7141. @"name" : @"select_cid",
  7142. @"refresh" : [NSNumber numberWithInteger:0],
  7143. @"value" : @"Sales_Order_Freight_Bill_To",
  7144. @"key_map" : @{
  7145. @"shipping_billto_cid" : @"customer_cid",
  7146. @"shipping_billto_contact" : @"customer_contact",
  7147. @"shipping_billto_email" : @"customer_email",
  7148. @"shipping_billto_ext" : @"customer_contact_ext",
  7149. @"shipping_billto_fax" : @"customer_fax",
  7150. @"shipping_billto_name" : @"customer_name",
  7151. @"shipping_billto_phone" : @"customer_phone"
  7152. }
  7153. };
  7154. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7155. [dic setValue:choose_dic forKey:@"item_0"];
  7156. // contact id
  7157. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7158. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7159. [contact_id_dic setValue:@"true" forKey:@"required"];
  7160. [dic setValue:contact_id_dic forKey:@"item_1"];
  7161. // company name
  7162. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7163. [dic setValue:company_name_dic forKey:@"item_2"];
  7164. // address
  7165. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7166. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7167. if ([ext_required integerValue]) {
  7168. [ext_dic setValue:@"true" forKey:@"required"];
  7169. }
  7170. [dic setValue:ext_dic forKey:@"item_3"];
  7171. // contact
  7172. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7173. [dic setValue:contact_dic forKey:@"item_4"];
  7174. // phone
  7175. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7176. [dic setValue:phone_dic forKey:@"item_5"];
  7177. // fax
  7178. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7179. [dic setValue:fax_dic forKey:@"item_6"];
  7180. // email
  7181. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7182. [dic setValue:email_dic forKey:@"item_7"];
  7183. }];
  7184. return dic;
  7185. }
  7186. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7187. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7188. // setting
  7189. NSDictionary *setting = params[@"setting"];
  7190. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7191. [dic setValue:hide forKey:@"hide"];
  7192. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7193. orderCode = [self translateSingleQuote:orderCode];
  7194. 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];
  7195. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7196. NSString *cid = [self textAtColumn:0 statement:stmt];
  7197. NSString *name = [self textAtColumn:1 statement:stmt];
  7198. NSString *ext = [self textAtColumn:2 statement:stmt];
  7199. NSString *contact = [self textAtColumn:3 statement:stmt];
  7200. NSString *email = [self textAtColumn:4 statement:stmt];
  7201. NSString *fax = [self textAtColumn:5 statement:stmt];
  7202. NSString *phone = [self textAtColumn:6 statement:stmt];
  7203. // count
  7204. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7205. // title
  7206. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7207. // hide
  7208. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7209. // choose
  7210. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7211. [choose_dic setValue:@"choose" forKey:@"aname"];
  7212. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7213. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7214. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7215. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7216. @"billing_contact" : @"receive_contact",
  7217. @"billing_email" : @"receive_email",
  7218. @"billing_ext" : @"receive_ext",
  7219. @"billing_fax" : @"receive_fax",
  7220. @"billing_name" : @"receive_name",
  7221. @"billing_phone" : @"receive_phone"},
  7222. @"type" : @"pull"};
  7223. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7224. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7225. @"type" : @"pull",
  7226. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7227. @"billing_contact" : @"customer_contact",
  7228. @"billing_email" : @"customer_email",
  7229. @"billing_ext" : @"customer_contact_ext",
  7230. @"billing_fax" : @"customer_fax",
  7231. @"billing_name" : @"customer_name",
  7232. @"billing_phone" : @"customer_phone"}
  7233. };
  7234. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7235. NSDictionary *select_bill_to_dic = @{
  7236. @"aname" : @"Select bill to",
  7237. @"name" : @"select_cid",
  7238. @"refresh" : [NSNumber numberWithInteger:0],
  7239. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7240. @"key_map" : @{
  7241. @"billing_cid" : @"customer_cid",
  7242. @"billing_contact" : @"customer_contact",
  7243. @"billing_email" : @"customer_email",
  7244. @"billing_ext" : @"customer_contact_ext",
  7245. @"billing_fax" : @"customer_fax",
  7246. @"billing_name" : @"customer_name",
  7247. @"billing_phone" : @"customer_phone"
  7248. }
  7249. };
  7250. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7251. [dic setValue:choose_dic forKey:@"item_0"];
  7252. // contact id
  7253. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7254. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7255. [contact_id_dic setValue:@"true" forKey:@"required"];
  7256. [dic setValue:contact_id_dic forKey:@"item_1"];
  7257. // company name
  7258. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7259. [dic setValue:company_name_dic forKey:@"item_2"];
  7260. // address
  7261. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7262. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7263. if ([ext_required integerValue]) {
  7264. [ext_dic setValue:@"true" forKey:@"required"];
  7265. }
  7266. [dic setValue:ext_dic forKey:@"item_3"];
  7267. // contact
  7268. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7269. [dic setValue:contact_dic forKey:@"item_4"];
  7270. // phone
  7271. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7272. [dic setValue:phone_dic forKey:@"item_5"];
  7273. // fax
  7274. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7275. [dic setValue:fax_dic forKey:@"item_6"];
  7276. // email
  7277. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7278. [dic setValue:email_dic forKey:@"item_7"];
  7279. }];
  7280. return dic;
  7281. }
  7282. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7283. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7284. // setting
  7285. NSDictionary *setting = params[@"setting"];
  7286. NSNumber *hide = setting[@"ReturnToHide"];
  7287. [dic setValue:hide forKey:@"hide"];
  7288. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7289. orderCode = [self translateSingleQuote:orderCode];
  7290. 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];
  7291. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7292. NSString *cid = [self textAtColumn:0 statement:stmt];
  7293. NSString *name = [self textAtColumn:1 statement:stmt];
  7294. NSString *ext = [self textAtColumn:2 statement:stmt];
  7295. NSString *contact = [self textAtColumn:3 statement:stmt];
  7296. NSString *email = [self textAtColumn:4 statement:stmt];
  7297. NSString *fax = [self textAtColumn:5 statement:stmt];
  7298. NSString *phone = [self textAtColumn:6 statement:stmt];
  7299. // count
  7300. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7301. // title
  7302. [dic setValue:@"Return To" forKey:@"title"];
  7303. // hide
  7304. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7305. // choose
  7306. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7307. [choose_dic setValue:@"choose" forKey:@"aname"];
  7308. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7309. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7310. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7311. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7312. @"returnto_contact" : @"sender_contact",
  7313. @"returnto_email" : @"sender_email",
  7314. @"returnto_ext" : @"sender_ext",
  7315. @"returnto_fax" : @"sender_fax",
  7316. @"returnto_name" : @"sender_name",
  7317. @"returnto_phone" : @"sender_phone"},
  7318. @"type" : @"pull"};
  7319. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7320. NSDictionary *select_return_to_dic = @{
  7321. @"aname" : @"Select return to",
  7322. @"name" : @"select_cid",
  7323. @"refresh" : [NSNumber numberWithInteger:0],
  7324. @"value" : @"Contact_Return_To",
  7325. @"key_map" : @{
  7326. @"returnto_cid" : @"customer_cid",
  7327. @"returnto_contact" : @"customer_contact",
  7328. @"returnto_email" : @"customer_email",
  7329. @"returnto_ext" : @"customer_contact_ext",
  7330. @"returnto_fax" : @"customer_fax",
  7331. @"returnto_name" : @"customer_name",
  7332. @"returnto_phone" : @"customer_phone"
  7333. }
  7334. };
  7335. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7336. [dic setValue:choose_dic forKey:@"item_0"];
  7337. // contact id
  7338. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7339. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7340. [contact_id_dic setValue:@"true" forKey:@"required"];
  7341. [dic setValue:contact_id_dic forKey:@"item_1"];
  7342. // company name
  7343. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7344. [dic setValue:company_name_dic forKey:@"item_2"];
  7345. // address
  7346. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7347. [dic setValue:ext_dic forKey:@"item_3"];
  7348. // contact
  7349. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7350. [dic setValue:contact_dic forKey:@"item_4"];
  7351. // phone
  7352. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7353. [dic setValue:phone_dic forKey:@"item_5"];
  7354. // fax
  7355. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7356. [dic setValue:fax_dic forKey:@"item_6"];
  7357. // email
  7358. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7359. [dic setValue:email_dic forKey:@"item_7"];
  7360. }];
  7361. return dic;
  7362. }
  7363. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7364. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7365. orderCode = [self translateSingleQuote:orderCode];
  7366. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7367. __block double TotalCuft = 0;
  7368. __block double TotalWeight = 0;
  7369. __block int TotalCarton = 0;
  7370. __block double payments = 0;
  7371. // setting
  7372. NSDictionary *setting = params[@"setting"];
  7373. NSNumber *hide = setting[@"ModelInformationHide"];
  7374. [dic setValue:hide forKey:@"hide"];
  7375. 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];
  7376. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7377. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7378. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7379. // item id
  7380. int item_id = sqlite3_column_int(stmt, 0);
  7381. // count
  7382. int item_count = sqlite3_column_int(stmt, 1);
  7383. // stockUom
  7384. int stockUom = sqlite3_column_int(stmt, 2);
  7385. // unit price
  7386. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7387. NSString* Price=nil;
  7388. if([str_price isEqualToString:@""])
  7389. {
  7390. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7391. if(price==nil)
  7392. Price=@"No Price.";
  7393. else
  7394. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7395. }
  7396. else
  7397. {
  7398. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7399. }
  7400. // discount
  7401. double discount = sqlite3_column_double(stmt, 4);
  7402. // name
  7403. NSString *name = [self textAtColumn:5 statement:stmt];
  7404. // description
  7405. NSString *description = [self textAtColumn:6 statement:stmt];
  7406. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7407. // line note
  7408. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7409. int avaulability = sqlite3_column_int(stmt, 8);
  7410. // img
  7411. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7412. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7413. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7414. if(combine != nil)
  7415. {
  7416. // int citem=0;
  7417. int bcount=[[combine valueForKey:@"count"] intValue];
  7418. for(int bc=0;bc<bcount;bc++)
  7419. {
  7420. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7421. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7422. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7423. subTotal += uprice * modulus * item_count;
  7424. }
  7425. }
  7426. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7427. [model_dic setValue:@"model" forKey:@"control"];
  7428. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7429. [model_dic setValue:description forKey:@"description"];
  7430. [model_dic setValue:line_note forKey:@"note"];
  7431. [model_dic setValue:img forKey:@"img_url"];
  7432. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7433. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7434. [model_dic setValue:Price forKey:@"unit_price"];
  7435. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7436. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7437. if (combine) {
  7438. [model_dic setValue:combine forKey:@"combine"];
  7439. }
  7440. // well,what under the row is the info for total
  7441. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7442. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7443. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7444. int carton=[bsubtotaljson[@"carton"] intValue];
  7445. TotalCuft += cuft;
  7446. TotalWeight += weight;
  7447. TotalCarton += carton;
  7448. payments += subTotal;
  7449. //---------------------------
  7450. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7451. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7452. }];
  7453. [dic setValue:@"Model Information" forKey:@"title"];
  7454. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7455. return dic;
  7456. }
  7457. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7458. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7459. [dic setValue:@"Remarks Content" forKey:@"title"];
  7460. // setting
  7461. NSDictionary *setting = params[@"setting"];
  7462. NSNumber *hide = setting[@"RemarksContentHide"];
  7463. [dic setValue:hide forKey:@"hide"];
  7464. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7465. orderCode = [self translateSingleQuote:orderCode];
  7466. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7467. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7468. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7469. int mustCall = sqlite3_column_int(stmt, 1);
  7470. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7471. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7472. NSDictionary *po_dic = @{
  7473. @"aname" : @"PO#",
  7474. @"control" : @"edit",
  7475. @"keyboard" : @"text",
  7476. @"name" : @"poNumber",
  7477. @"value" : poNumber
  7478. };
  7479. NSDictionary *must_call_dic = @{
  7480. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7481. @"control" : @"switch",
  7482. @"name" : @"must_call",
  7483. @"value" : mustCall ? @"true" : @"false"
  7484. };
  7485. NSDictionary *general_notes_dic = @{
  7486. @"aname" : @"General notes",
  7487. @"control" : @"text_view",
  7488. @"keyboard" : @"text",
  7489. @"name" : @"comments",
  7490. @"value" : generalNotes
  7491. };
  7492. NSDictionary *internal_notes_dic = @{
  7493. @"aname" : @"Internal notes",
  7494. @"control" : @"text_view",
  7495. @"keyboard" : @"text",
  7496. @"name" : @"internal_notes",
  7497. @"value" : internalNotes
  7498. };
  7499. [dic setValue:po_dic forKey:@"item_0"];
  7500. [dic setValue:must_call_dic forKey:@"item_1"];
  7501. [dic setValue:general_notes_dic forKey:@"item_2"];
  7502. // [dic setValue:internal_notes_dic forKey:@"item_3"];
  7503. }];
  7504. [dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7505. return dic;
  7506. }
  7507. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7508. // params
  7509. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7510. orderCode = [self translateSingleQuote:orderCode];
  7511. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7512. // setting
  7513. NSDictionary *setting = params[@"setting"];
  7514. NSNumber *hide = setting[@"OrderTotalHide"];
  7515. [dic setValue:hide forKey:@"hide"];
  7516. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7517. __block double lift_gate_value = 0;
  7518. __block double handling_fee = 0;
  7519. __block double shipping = 0;
  7520. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7521. int lift_gate = sqlite3_column_int(stmt, 0);
  7522. lift_gate_value = sqlite3_column_double(stmt, 1);
  7523. shipping = sqlite3_column_double(stmt, 2);
  7524. handling_fee = sqlite3_column_double(stmt, 3);
  7525. if (!lift_gate) {
  7526. lift_gate_value = 0;
  7527. }
  7528. }];
  7529. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7530. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7531. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7532. double payments = [[total valueForKey:@"payments"] doubleValue];
  7533. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7534. double totalPrice = payments;
  7535. [dic setValue:@"Order Total" forKey:@"title"];
  7536. NSDictionary *payments_dic = @{
  7537. @"align" : @"right",
  7538. @"aname" : @"Payments/Credits",
  7539. @"control" : @"text",
  7540. @"name" : @"paymentsAndCredits",
  7541. @"type" : @"price",
  7542. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7543. };
  7544. [dic setValue:payments_dic forKey:@"item_0"];
  7545. // version 1.71 remove
  7546. // NSDictionary *handling_fee_dic = @{
  7547. // @"align" : @"right",
  7548. // @"aname" : @"Handling Fee",
  7549. // @"control" : @"text",
  7550. // @"name" : @"handling_fee_value",
  7551. // @"required" : @"true",
  7552. // @"type" : @"price",
  7553. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7554. // };
  7555. NSDictionary *shipping_dic = @{
  7556. @"align" : @"right",
  7557. @"aname" : @"Shipping*",
  7558. @"control" : @"text",
  7559. @"name" : @"shipping",
  7560. @"required" : @"true",
  7561. @"type" : @"price",
  7562. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7563. };
  7564. NSDictionary *lift_gate_dic = @{
  7565. @"align" : @"right",
  7566. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7567. @"control" : @"text",
  7568. @"name" : @"lift_gate_value",
  7569. @"required" : @"true",
  7570. @"type" : @"price",
  7571. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7572. };
  7573. int item_count = 1;
  7574. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7575. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7576. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7577. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7578. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7579. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7580. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7581. } else {
  7582. }
  7583. }
  7584. // version 1.71 remove
  7585. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7586. NSDictionary *total_price_dic = @{
  7587. @"align" : @"right",
  7588. @"aname" : @"Total",
  7589. @"control" : @"text",
  7590. @"name" : @"totalPrice",
  7591. @"type" : @"price",
  7592. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7593. };
  7594. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7595. NSDictionary *total_cuft_dic = @{
  7596. @"align" : @"right",
  7597. @"aname" : @"Total Cuft",
  7598. @"control" : @"text",
  7599. @"name" : @"",
  7600. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7601. };
  7602. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7603. NSDictionary *total_weight_dic = @{
  7604. @"align" : @"right",
  7605. @"aname" : @"Total Weight",
  7606. @"control" : @"text",
  7607. @"name" : @"",
  7608. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7609. };
  7610. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7611. NSDictionary *total_carton_dic = @{
  7612. @"align" : @"right",
  7613. @"aname" : @"Total Carton",
  7614. @"control" : @"text",
  7615. @"name" : @"",
  7616. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7617. };
  7618. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7619. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7620. return dic;
  7621. }
  7622. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7623. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7624. orderCode = [self translateSingleQuote:orderCode];
  7625. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7626. // setting
  7627. NSDictionary *setting = params[@"setting"];
  7628. NSNumber *hide = setting[@"SignatureHide"];
  7629. [dic setValue:hide forKey:@"hide"];
  7630. [dic setValue:@"Signature" forKey:@"title"];
  7631. __block NSString *pic_path = @"";
  7632. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7633. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7634. pic_path = [self textAtColumn:0 statement:stmt];
  7635. }];
  7636. NSDictionary *pic_dic = @{
  7637. @"aname" : @"Signature",
  7638. @"avalue" :pic_path,
  7639. @"control" : @"signature",
  7640. @"name" : @"sign_picpath",
  7641. @"value" : pic_path
  7642. };
  7643. [dic setValue:pic_dic forKey:@"item_0"];
  7644. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7645. return dic;
  7646. }
  7647. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7648. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7649. // setting
  7650. NSDictionary *setting = params[@"setting"];
  7651. NSNumber *hide = setting[@"ShippingMethodHide"];
  7652. [dic setValue:hide forKey:@"hide"];
  7653. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7654. orderCode = [self translateSingleQuote:orderCode];
  7655. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7656. __block NSString *logist = @"";
  7657. __block NSString *lift_gate = @"";
  7658. __block int lift_gate_integer = 0;
  7659. __block NSString *logistic_note = @"";
  7660. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7661. logist = [self textAtColumn:0 statement:stmt];
  7662. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7663. logistic_note = [self textAtColumn:2 statement:stmt];
  7664. }];
  7665. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7666. [dic setValue:@"Shipping Method" forKey:@"title"];
  7667. // val_0
  7668. int PERSONAL_PICK_UP_check = 0;
  7669. int USE_MY_CARRIER_check = 0;
  7670. NSString *logistic_note_text = @"";
  7671. int will_call_check = 0;
  7672. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7673. will_call_check = 1;
  7674. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7675. PERSONAL_PICK_UP_check = 1;
  7676. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7677. USE_MY_CARRIER_check = 1;
  7678. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7679. logistic_note = [logistic_note_array firstObject];
  7680. if (logistic_note_array.count > 1) {
  7681. logistic_note_text = [logistic_note_array lastObject];
  7682. }
  7683. }
  7684. }
  7685. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7686. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7687. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7688. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7689. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7690. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7691. NSDictionary *val0_subItem_item0 = @{
  7692. @"aname" : @"Option",
  7693. @"cadedate" : @{
  7694. @"count" : [NSNumber numberWithInteger:2],
  7695. @"val_0" : @{
  7696. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7697. @"refresh" : [NSNumber numberWithInteger:0],
  7698. @"value" : @"PERSONAL PICK UP",
  7699. @"value_id" : @"PERSONAL PICK UP"
  7700. },
  7701. @"val_1" : @{
  7702. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7703. @"refresh" : [NSNumber numberWithInteger:0],
  7704. @"sub_item" : @{
  7705. @"count" : [NSNumber numberWithInteger:1],
  7706. @"item_0" : @{
  7707. @"aname" : @"BOL",
  7708. @"control" : @"edit",
  7709. @"keyboard" : @"text",
  7710. @"name" : @"logist_note_text",
  7711. @"refresh" : [NSNumber numberWithInteger:0],
  7712. @"required" : @"false",
  7713. @"value" : logistic_note_text
  7714. }
  7715. },
  7716. @"value" : @"USE MY CARRIER",
  7717. @"value_id" : @"USE MY CARRIER"
  7718. }
  7719. },
  7720. @"control" : @"enum",
  7721. @"name" : @"logistic_note",
  7722. @"required" : @"true",
  7723. @"single_select" : @"true"
  7724. };
  7725. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7726. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7727. // val_1
  7728. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7729. if([lift_gate isEqualToString:@""]) {
  7730. if (lift_gate_integer == 1) {
  7731. lift_gate = @"true";
  7732. } else {
  7733. lift_gate = @"false";
  7734. }
  7735. }
  7736. int common_carrier_check = 0;
  7737. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7738. common_carrier_check = 1;
  7739. [params setValue:lift_gate forKey:@"lift_gate"];
  7740. }
  7741. NSDictionary *val_1 = @{
  7742. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7743. @"sub_item" : @{
  7744. @"count" : [NSNumber numberWithInteger:1],
  7745. @"item_0" : @{
  7746. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7747. @"control" : @"switch",
  7748. @"name" : @"lift_gate",
  7749. @"refresh" : [NSNumber numberWithInteger:1],
  7750. @"required" : @"true",
  7751. @"value" : lift_gate
  7752. }
  7753. },
  7754. @"value" : @"COMMON CARRIER",
  7755. @"value_id" : @"COMMON CARRIER"
  7756. };
  7757. // cadedate
  7758. NSDictionary *cadedate = @{
  7759. @"count" : [NSNumber numberWithInteger:2],
  7760. @"val_0" : val_0,
  7761. @"val_1" : val_1
  7762. };
  7763. // item_0
  7764. NSMutableDictionary *item_0 = @{
  7765. @"aname" : @"Shipping",
  7766. @"cadedate" : cadedate,
  7767. @"control" : @"enum",
  7768. @"name" : @"logist",
  7769. @"refresh" : [NSNumber numberWithInteger:1],
  7770. @"single_select" : @"true",
  7771. }.mutableCopy;
  7772. NSNumber *required = setting[@"ShippingMethodRequire"];
  7773. if ([required integerValue]) {
  7774. [item_0 setValue:@"true" forKey:@"required"];
  7775. }
  7776. [dic setValue:item_0 forKey:@"item_0"];
  7777. if ([logist isEqualToString:@"WILL CALL"]) {
  7778. [dic removeObjectForKey:@"lift_gate"];
  7779. }
  7780. return dic;
  7781. }
  7782. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7783. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7784. orderCode = [self translateSingleQuote:orderCode];
  7785. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7786. __block int submit_as_integer = 0;
  7787. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7788. submit_as_integer = sqlite3_column_int(stmt, 0);
  7789. }];
  7790. int check11 = 0;
  7791. int check10 = 0;
  7792. if (submit_as_integer == 11) {
  7793. check11 = 1;
  7794. }
  7795. if (submit_as_integer == 10) {
  7796. check10 = 1;
  7797. }
  7798. // section_0
  7799. NSMutableDictionary *dic = @{
  7800. @"count" : @(1),
  7801. @"item_0" : @{
  7802. @"aname" : @"Submit Order As",
  7803. @"cadedate" : @{
  7804. @"count" : @(2),
  7805. @"val_0" : @{
  7806. @"check" : [NSNumber numberWithInteger:check11],
  7807. @"value" : @"Sales Order",
  7808. @"value_id" : @(11)
  7809. },
  7810. @"val_1" : @{
  7811. @"check" : [NSNumber numberWithInteger:check10],
  7812. @"value" : @"Quote",
  7813. @"value_id" : @(10)
  7814. }
  7815. },
  7816. @"control" : @"enum",
  7817. @"name" : @"erpOrderStatus",
  7818. @"required" : @"true",
  7819. @"single_select" : @"true"
  7820. },
  7821. @"title" : @"Order Type"
  7822. }.mutableCopy;
  7823. // setting
  7824. NSDictionary *setting = params[@"setting"];
  7825. NSNumber *hide = setting[@"OrderTypeHide"];
  7826. [dic setValue:hide forKey:@"hide"];
  7827. return dic;
  7828. }
  7829. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7830. countryCode = [self translateSingleQuote:countryCode];
  7831. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7832. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7833. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7834. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7835. if (name == NULL) {
  7836. name = "";
  7837. }
  7838. if (code == NULL) {
  7839. code = "";
  7840. }
  7841. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7842. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7843. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7844. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7845. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7846. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7847. }
  7848. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7849. [container setValue:stateDic forKey:key];
  7850. }] mutableCopy];
  7851. [ret removeObjectForKey:@"result"];
  7852. // failure 可以不用了,一样的
  7853. if (ret.allKeys.count == 0) {
  7854. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7855. [stateDic setValue:@"Other" forKey:@"value"];
  7856. [stateDic setValue:@"" forKey:@"value_id"];
  7857. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7858. if (state_code && [@"" isEqualToString:state_code]) {
  7859. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7860. }
  7861. NSString *key = [NSString stringWithFormat:@"val_0"];
  7862. [ret setValue:stateDic forKey:key];
  7863. }
  7864. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7865. return ret;
  7866. }
  7867. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7868. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7869. orderCode = [self translateSingleQuote:orderCode];
  7870. 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];
  7871. __block NSString *payType = @"";
  7872. __block NSString *firstName = @"";
  7873. __block NSString *lastName = @"";
  7874. __block NSString *addr1 = @"";
  7875. __block NSString *addr2 = @"";
  7876. __block NSString *zipcode = @"";
  7877. __block NSString *cardType = @"";
  7878. __block NSString *cardNumber = @"";
  7879. __block NSString *securityCode = @"";
  7880. __block NSString *month = @"";
  7881. __block NSString *year = @"";
  7882. __block NSString *city = @"";
  7883. __block NSString *state = @"";
  7884. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7885. payType = [self textAtColumn:0 statement:stmt];
  7886. firstName = [self textAtColumn:1 statement:stmt];
  7887. lastName = [self textAtColumn:2 statement:stmt];
  7888. addr1 = [self textAtColumn:3 statement:stmt];
  7889. addr2 = [self textAtColumn:4 statement:stmt];
  7890. zipcode = [self textAtColumn:5 statement:stmt];
  7891. cardType = [self textAtColumn:6 statement:stmt];
  7892. cardNumber = [self textAtColumn:7 statement:stmt];
  7893. securityCode = [self textAtColumn:8 statement:stmt];
  7894. month = [self textAtColumn:9 statement:stmt];
  7895. year = [self textAtColumn:10 statement:stmt];
  7896. city = [self textAtColumn:11 statement:stmt];
  7897. state = [self textAtColumn:12 statement:stmt];
  7898. }];
  7899. NSString *required = @"true";
  7900. // setting
  7901. NSDictionary *setting = params[@"setting"];
  7902. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7903. if ([requiredNumber integerValue]) {
  7904. required = @"true";
  7905. } else {
  7906. required = @"false";
  7907. }
  7908. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7909. // create table if not exists payment_type (_id integer primary key,type_id text,pay_type text,sort int);
  7910. NSString *pay_type_sql = @"select type_id,pay_type,sort_id from payment_type order by sort_id asc";
  7911. [iSalesDB jk_query:pay_type_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7912. NSString *type_id = [self textAtColumn:0 statement:stmt];
  7913. NSString *type = [self textAtColumn:1 statement:stmt];
  7914. NSMutableDictionary *val = @{
  7915. @"check" : [payType isEqualToString:type_id] ? @(1) : @(0),
  7916. @"value" : type,
  7917. @"value_id" : type_id
  7918. }.mutableCopy;
  7919. if ([type_id isEqualToString:@"Visa/Master"] || [type_id isEqualToString:@"Credit Card"]) {
  7920. NSDictionary *sub_item = @{
  7921. @"count" : @(3),
  7922. @"item_0" : @{
  7923. @"aname" : @"choose",
  7924. @"control" : @"multi_action",
  7925. @"count" : @(1),
  7926. @"item_0" : @{
  7927. @"aname" : @"Same as customer",
  7928. @"key_map" : @{
  7929. @"credit_card_address1" : @"customer_address1",
  7930. @"credit_card_address2" : @"customer_address2",
  7931. @"credit_card_city" : @"customer_city",
  7932. @"credit_card_first_name" : @"customer_first_name",
  7933. @"credit_card_last_name" : @"customer_last_name",
  7934. @"credit_card_state" : @"customer_state",
  7935. @"credit_card_zipcode" : @"customer_zipcode"
  7936. },
  7937. @"type" : @"pull"
  7938. }
  7939. },
  7940. @"item_1" : @{
  7941. @"aname" : @"",
  7942. @"color" : @"red",
  7943. @"control" : @"text",
  7944. @"name" : @"",
  7945. @"value" : @"USA Credit cards only"
  7946. },
  7947. @"item_2" : @{
  7948. @"aname" : @"Fill",
  7949. @"cadedate" : @{
  7950. @"count" : @(2),
  7951. @"val_0" : @{
  7952. @"check" : @(1),
  7953. @"sub_item" : @{
  7954. @"count" : @(11),
  7955. @"item_0" : @{
  7956. @"aname" : @"Type",
  7957. @"cadedate" : @{
  7958. @"count" : @(2),
  7959. @"val_0" : @{
  7960. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7961. @"value" : @"VISA",
  7962. @"value_id" : @"VISA"/*@(0)*/
  7963. },
  7964. @"val_1" : @{
  7965. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7966. @"value" : @"MASTER CARD",
  7967. @"value_id" : @"MASTER CARD"/*@(1)*/
  7968. }
  7969. },
  7970. @"control" : @"enum",
  7971. @"name" : @"credit_card_type",
  7972. @"required" : @"true",
  7973. @"single_select" : @"true"
  7974. },
  7975. @"item_1" : @{
  7976. @"aname" : @"Number",
  7977. @"control" : @"edit",
  7978. @"keyboard" : @"int",
  7979. @"length" : @"16",
  7980. @"name" : @"credit_card_number",
  7981. @"required" : @"true",
  7982. @"value" : cardNumber
  7983. },
  7984. @"item_10" : @{
  7985. @"aname" : @"State",
  7986. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7987. @"control" : @"enum",
  7988. @"enum" : @"true",
  7989. @"name" : @"credit_card_state",
  7990. @"required" : @"true",
  7991. @"single_select" : @"true"
  7992. },
  7993. @"item_2" : @{
  7994. @"aname" : @"Expiration Date",
  7995. @"control" : @"monthpicker",
  7996. @"name" : @"credit_card_expiration",
  7997. @"required" : @"true",
  7998. @"type" : @"date",
  7999. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8000. },
  8001. @"item_3" : @{
  8002. @"aname" : @"Security Code",
  8003. @"control" : @"edit",
  8004. @"keyboard" : @"int",
  8005. @"length" : @"3",
  8006. @"name" : @"credit_card_security_code",
  8007. @"required" : @"true",
  8008. @"value" : securityCode
  8009. },
  8010. @"item_4" : @{
  8011. @"aname" : @"First Name",
  8012. @"control" : @"edit",
  8013. @"keyboard" : @"text",
  8014. @"name" : @"credit_card_first_name",
  8015. @"required" : @"true",
  8016. @"value" : firstName
  8017. },
  8018. @"item_5" : @{
  8019. @"aname" : @"Last Name",
  8020. @"control" : @"edit",
  8021. @"keyboard" : @"text",
  8022. @"name" : @"credit_card_last_name",
  8023. @"required" : @"true",
  8024. @"value" : lastName
  8025. },
  8026. @"item_6" : @{
  8027. @"aname" : @"Address 1",
  8028. @"control" : @"edit",
  8029. @"keyboard" : @"text",
  8030. @"name" : @"credit_card_address1",
  8031. @"required" : @"true",
  8032. @"value" : addr1
  8033. },
  8034. @"item_7" : @{
  8035. @"aname" : @"Address 2",
  8036. @"control" : @"edit",
  8037. @"keyboard" : @"text",
  8038. @"name" : @"credit_card_address2",
  8039. @"value" : addr2
  8040. },
  8041. @"item_8" : @{
  8042. @"aname" : @"zip code",
  8043. @"control" : @"edit",
  8044. @"keyboard" : @"text",
  8045. @"name" : @"credit_card_zipcode",
  8046. @"required" : @"true",
  8047. @"value" : zipcode
  8048. },
  8049. @"item_9" : @{
  8050. @"aname" : @"City",
  8051. @"control" : @"edit",
  8052. @"keyboard" : @"text",
  8053. @"name" : @"credit_card_city",
  8054. @"required" : @"true",
  8055. @"value" : city
  8056. }
  8057. },
  8058. @"value" : @"Fill Now",
  8059. @"value_id" : @""
  8060. },
  8061. @"val_1" : @{
  8062. @"check" : @(0),
  8063. @"value" : @"Fill Later",
  8064. @"value_id" : @""
  8065. }
  8066. },
  8067. @"control" : @"enum",
  8068. @"name" : @"",
  8069. @"single_select" : @"true"
  8070. }
  8071. };
  8072. [val setObject:sub_item forKey:@"sub_item"];
  8073. }
  8074. [cadedate setObject:val forKey:[NSString stringWithFormat:@"val_%ld",(*count)]];
  8075. [cadedate setObject:[NSNumber numberWithInteger:(++*count)] forKey:@"count"];
  8076. }];
  8077. // "section_2"
  8078. NSMutableDictionary *dic = @{
  8079. @"count" : @(1),
  8080. @"item_0" : @{
  8081. @"aname" : @"Payment",
  8082. @"required" : required,
  8083. @"cadedate" : cadedate,
  8084. // @{
  8085. // @"count" : @(6),
  8086. // @"val_3" : @{
  8087. // @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  8088. // @"value" : @"Check",
  8089. // @"value_id" : @"Check"
  8090. // },
  8091. // @"val_2" : @{
  8092. // @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  8093. // @"value" : @"Cash",
  8094. // @"value_id" : @"Cash"
  8095. // },
  8096. // @"val_1" : @{
  8097. // @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  8098. // @"value" : @"NET 60",
  8099. // @"value_id" : @"NET 30"
  8100. // },
  8101. // @"val_4" : @{
  8102. // @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  8103. // @"value" : @"Wire Transfer",
  8104. // @"value_id" : @"Wire Transfer"
  8105. // },
  8106. // @"val_0" : @{
  8107. // @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  8108. // @"sub_item" : @{
  8109. // @"count" : @(3),
  8110. // @"item_0" : @{
  8111. // @"aname" : @"choose",
  8112. // @"control" : @"multi_action",
  8113. // @"count" : @(1),
  8114. // @"item_0" : @{
  8115. // @"aname" : @"Same as customer",
  8116. // @"key_map" : @{
  8117. // @"credit_card_address1" : @"customer_address1",
  8118. // @"credit_card_address2" : @"customer_address2",
  8119. // @"credit_card_city" : @"customer_city",
  8120. // @"credit_card_first_name" : @"customer_first_name",
  8121. // @"credit_card_last_name" : @"customer_last_name",
  8122. // @"credit_card_state" : @"customer_state",
  8123. // @"credit_card_zipcode" : @"customer_zipcode"
  8124. // },
  8125. // @"type" : @"pull"
  8126. // }
  8127. // },
  8128. // @"item_1" : @{
  8129. // @"aname" : @"",
  8130. // @"color" : @"red",
  8131. // @"control" : @"text",
  8132. // @"name" : @"",
  8133. // @"value" : @"USA Credit cards only"
  8134. // },
  8135. // @"item_2" : @{
  8136. // @"aname" : @"Fill",
  8137. // @"cadedate" : @{
  8138. // @"count" : @(2),
  8139. // @"val_0" : @{
  8140. // @"check" : @(1),
  8141. // @"sub_item" : @{
  8142. // @"count" : @(11),
  8143. // @"item_0" : @{
  8144. // @"aname" : @"Type",
  8145. // @"cadedate" : @{
  8146. // @"count" : @(2),
  8147. // @"val_0" : @{
  8148. // @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  8149. // @"value" : @"VISA",
  8150. // @"value_id" : @(0)
  8151. // },
  8152. // @"val_1" : @{
  8153. // @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  8154. // @"value" : @"MASTER CARD",
  8155. // @"value_id" : @(1)
  8156. // }
  8157. // },
  8158. // @"control" : @"enum",
  8159. // @"name" : @"credit_card_type",
  8160. // @"required" : @"true",
  8161. // @"single_select" : @"true"
  8162. // },
  8163. // @"item_1" : @{
  8164. // @"aname" : @"Number",
  8165. // @"control" : @"edit",
  8166. // @"keyboard" : @"int",
  8167. // @"length" : @"16",
  8168. // @"name" : @"credit_card_number",
  8169. // @"required" : @"true",
  8170. // @"value" : cardNumber
  8171. // },
  8172. // @"item_10" : @{
  8173. // @"aname" : @"State",
  8174. // @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  8175. // @"control" : @"enum",
  8176. // @"enum" : @"true",
  8177. // @"name" : @"credit_card_state",
  8178. // @"required" : @"true",
  8179. // @"single_select" : @"true"
  8180. // },
  8181. // @"item_2" : @{
  8182. // @"aname" : @"Expiration Date",
  8183. // @"control" : @"monthpicker",
  8184. // @"name" : @"credit_card_expiration",
  8185. // @"required" : @"true",
  8186. // @"type" : @"date",
  8187. // @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  8188. // },
  8189. // @"item_3" : @{
  8190. // @"aname" : @"Security Code",
  8191. // @"control" : @"edit",
  8192. // @"keyboard" : @"int",
  8193. // @"length" : @"3",
  8194. // @"name" : @"credit_card_security_code",
  8195. // @"required" : @"true",
  8196. // @"value" : securityCode
  8197. // },
  8198. // @"item_4" : @{
  8199. // @"aname" : @"First Name",
  8200. // @"control" : @"edit",
  8201. // @"keyboard" : @"text",
  8202. // @"name" : @"credit_card_first_name",
  8203. // @"required" : @"true",
  8204. // @"value" : firstName
  8205. // },
  8206. // @"item_5" : @{
  8207. // @"aname" : @"Last Name",
  8208. // @"control" : @"edit",
  8209. // @"keyboard" : @"text",
  8210. // @"name" : @"credit_card_last_name",
  8211. // @"required" : @"true",
  8212. // @"value" : lastName
  8213. // },
  8214. // @"item_6" : @{
  8215. // @"aname" : @"Address 1",
  8216. // @"control" : @"edit",
  8217. // @"keyboard" : @"text",
  8218. // @"name" : @"credit_card_address1",
  8219. // @"required" : @"true",
  8220. // @"value" : addr1
  8221. // },
  8222. // @"item_7" : @{
  8223. // @"aname" : @"Address 2",
  8224. // @"control" : @"edit",
  8225. // @"keyboard" : @"text",
  8226. // @"name" : @"credit_card_address2",
  8227. // @"value" : addr2
  8228. // },
  8229. // @"item_8" : @{
  8230. // @"aname" : @"zip code",
  8231. // @"control" : @"edit",
  8232. // @"keyboard" : @"text",
  8233. // @"name" : @"credit_card_zipcode",
  8234. // @"required" : @"true",
  8235. // @"value" : zipcode
  8236. // },
  8237. // @"item_9" : @{
  8238. // @"aname" : @"City",
  8239. // @"control" : @"edit",
  8240. // @"keyboard" : @"text",
  8241. // @"name" : @"credit_card_city",
  8242. // @"required" : @"true",
  8243. // @"value" : city
  8244. // }
  8245. // },
  8246. // @"value" : @"Fill Now",
  8247. // @"value_id" : @""
  8248. // },
  8249. // @"val_1" : @{
  8250. // @"check" : @(0),
  8251. // @"value" : @"Fill Later",
  8252. // @"value_id" : @""
  8253. // }
  8254. // },
  8255. // @"control" : @"enum",
  8256. // @"name" : @"",
  8257. // @"single_select" : @"true"
  8258. // }
  8259. // },
  8260. // @"value" : @"Visa/Master",
  8261. // @"value_id" : @"Credit Card"
  8262. // },
  8263. // @"val_5" : @{
  8264. // @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  8265. // @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  8266. // @"value_id" : @"FOLLOW EXISTING"
  8267. // }
  8268. // },
  8269. @"control" : @"enum",
  8270. @"name" : @"paymentType",
  8271. @"single_select" : @"true"
  8272. },
  8273. @"title" : @"Payment Information"
  8274. }.mutableCopy;
  8275. NSNumber *hide = setting[@"PaymentInformationHide"];
  8276. [dic setValue:hide forKey:@"hide"];
  8277. return dic;
  8278. }
  8279. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  8280. // params
  8281. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8282. orderCode = [self translateSingleQuote:orderCode];
  8283. // 缺货检查
  8284. NSString *out_of_stock_SQL = [NSString stringWithFormat:@"select p.availability,c.item_count from product as p left join offline_cart as c on p.product_id = c.product_id where c.so_no = '%@';",orderCode];
  8285. __block BOOL outOfStock = NO;
  8286. sqlite3 *database = db;
  8287. if (!db) {
  8288. database = [iSalesDB get_db];
  8289. }
  8290. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8291. int availability = sqlite3_column_int(stmt, 0);
  8292. int item_qty = sqlite3_column_int(stmt, 1);
  8293. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  8294. }];
  8295. if (!db) {
  8296. [iSalesDB close_db:database];
  8297. }
  8298. return outOfStock;
  8299. }
  8300. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  8301. {
  8302. sqlite3 *db = [iSalesDB get_db];
  8303. // params
  8304. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8305. orderCode = [self translateSingleQuote:orderCode];
  8306. // 缺货检查
  8307. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8308. if (!appDelegate.can_create_backorder) {
  8309. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  8310. if (out_of_stock) {
  8311. [iSalesDB close_db:db];
  8312. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  8313. [resultDic setObject:[NSString stringWithFormat:@"some item(s) are out of stock,please remove them from cart before placing order with %@ or try again after sync",COMPANY_SHORT_NAME] forKey:@"err_msg"];
  8314. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  8315. return [RAConvertor dict2data:resultDic];
  8316. }
  8317. }
  8318. // UISetting
  8319. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8320. NSString *cachefolder = [paths objectAtIndex:0];
  8321. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8322. NSData* json =nil;
  8323. json=[NSData dataWithContentsOfFile:img_cache];
  8324. NSError *error=nil;
  8325. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8326. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8327. [params setObject:setting forKey:@"setting"];
  8328. int section_count = 0;
  8329. // 0 Order Type 1 Shipping Method 2 Payment Information
  8330. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8331. // 0 Order Type
  8332. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8333. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8334. [ret setValue:order_type_dic forKey:key0];
  8335. // 1 Shipping Method
  8336. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8337. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8338. [ret setValue:shipping_method_dic forKey:key1];
  8339. // 2 Payment Information
  8340. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8341. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8342. [ret setValue:payment_info_dic forKey:key2];
  8343. // 3 Customer
  8344. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8345. NSDictionary *customer_dic = [self customerDic:params db:db];
  8346. [ret setValue:customer_dic forKey:key3];
  8347. // 4 Ship To
  8348. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8349. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8350. [ret setValue:ship_to_dic forKey:key4];
  8351. // 5 Ship From
  8352. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8353. if (![shipFromDisable integerValue]) {
  8354. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8355. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8356. [ret setValue:ship_from_dic forKey:key5];
  8357. }
  8358. // 6 Freight Bill To
  8359. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8360. if (![freightBillToDisable integerValue]) {
  8361. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8362. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8363. [ret setValue:freight_bill_to forKey:key6];
  8364. }
  8365. // 7 Merchandise Bill To
  8366. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8367. if (![merchandiseBillToDisable integerValue]) {
  8368. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8369. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8370. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8371. }
  8372. // 8 Return To
  8373. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8374. if (![returnToDisable integerValue]) {
  8375. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8376. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8377. [ret setValue:return_to_dic forKey:key8];
  8378. }
  8379. // 9 Model Information
  8380. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8381. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8382. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8383. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8384. [ret setValue:model_info_dic forKey:key9];
  8385. // 10 Remarks Content
  8386. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8387. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8388. [ret setValue:remarks_content_dic forKey:key10];
  8389. // 11 Order Total
  8390. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8391. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8392. [ret setValue:order_total_dic forKey:key11];
  8393. // 12 Signature
  8394. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8395. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8396. [ret setValue:sign_dic forKey:key12];
  8397. [ret setValue:@(section_count) forKey:@"section_count"];
  8398. [iSalesDB close_db:db];
  8399. return [RAConvertor dict2data:ret];
  8400. // return nil;
  8401. }
  8402. #pragma mark addr editor
  8403. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8404. NSMutableDictionary *new_item = [item mutableCopy];
  8405. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8406. return new_item;
  8407. }
  8408. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8409. {
  8410. // "is_subaction" = true;
  8411. // orderCode = MOB1608240002;
  8412. // password = 123456;
  8413. // "subaction_tag" = 1;
  8414. // user = EvanK;
  8415. // {
  8416. // "is_subaction" = true;
  8417. // orderCode = MOB1608240002;
  8418. // password = 123456;
  8419. // "refresh_trigger" = zipcode;
  8420. // "subaction_tag" = 1;
  8421. // user = EvanK;
  8422. // }
  8423. NSString *country_code = nil;
  8424. NSString *zipCode = nil;
  8425. NSString *stateCode = nil;
  8426. NSString *city = nil;
  8427. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8428. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8429. NSString *code_id = params[@"country"];
  8430. country_code = [self countryCodeByid:code_id];
  8431. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8432. NSString *zip_code = params[@"zipcode"];
  8433. // 剔除全部为空格
  8434. int spaceCount = 0;
  8435. for (int i = 0; i < zip_code.length; i++) {
  8436. if ([zip_code characterAtIndex:i] == ' ') {
  8437. spaceCount++;
  8438. }
  8439. }
  8440. if (spaceCount == zip_code.length) {
  8441. zip_code = @"";
  8442. }
  8443. zipCode = zip_code;
  8444. if (zipCode.length > 0) {
  8445. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8446. country_code = [dic valueForKey:@"country_code"];
  8447. if (!country_code) {
  8448. // country_code = @"US";
  8449. NSString *code_id = params[@"country"];
  8450. country_code = [self countryCodeByid:code_id];
  8451. }
  8452. stateCode = [dic valueForKey:@"state_code"];
  8453. if(!stateCode.length) {
  8454. stateCode = params[@"state"];
  8455. }
  8456. city = [dic valueForKey:@"city"];
  8457. if (!city.length) {
  8458. city = params[@"city"];
  8459. }
  8460. // zip code
  8461. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8462. // [zipDic setValue:zipCode forKey:@"value"];
  8463. // [section_0 setValue:zipDic forKey:@"item_11"];
  8464. } else {
  8465. NSString *code_id = params[@"country"];
  8466. country_code = [self countryCodeByid:code_id];
  8467. stateCode = params[@"state"];
  8468. city = params[@"city"];
  8469. }
  8470. }
  8471. }
  8472. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8473. // [ret removeObjectForKey:@"up_params"];
  8474. [ret setObject:@"New Address" forKey:@"title"];
  8475. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8476. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8477. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8478. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8479. // [country_dic removeObjectForKey:@"refresh"];
  8480. // [country_dic removeObjectForKey:@"restore"];
  8481. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8482. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8483. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8484. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8485. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8486. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8487. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8488. // [zip_code_dic removeObjectForKey:@"refresh"];
  8489. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8490. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8491. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8492. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8493. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8494. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8495. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8496. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8497. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8498. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8499. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8500. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8501. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8502. // country
  8503. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8504. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8505. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8506. // state
  8507. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8508. // NSDictionary *tmpDic = @{
  8509. // @"value" : @"Other",
  8510. // @"value_id" : @"",
  8511. // @"check" : [NSNumber numberWithInteger:0]
  8512. // };
  8513. //
  8514. // for (int i = 0; i < allState.allKeys.count; i++) {
  8515. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8516. //
  8517. // NSDictionary *tmp = allState[key];
  8518. // [allState setValue:tmpDic forKey:key];
  8519. // tmpDic = tmp;
  8520. // }
  8521. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8522. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8523. [ret setValue:new_section_0 forKey:@"section_0"];
  8524. return [RAConvertor dict2data:ret];
  8525. }
  8526. #pragma mark save addr
  8527. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8528. {
  8529. // NSString *companyName = [self valueInParams:params key:@"company"];
  8530. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8531. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8532. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8533. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8534. // NSString *countryId = [self valueInParams:params key:@"country"];
  8535. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8536. // NSString *city = [self valueInParams:params key:@"city"];
  8537. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8538. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8539. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8540. // NSString *phone = [self valueInParams:params key:@"phone"];
  8541. // NSString *fax = [self valueInParams:params key:@"fax"];
  8542. // NSString *email = [self valueInParams:params key:@"email"];
  8543. return [self offline_saveContact:params update:NO isCustomer:NO];
  8544. }
  8545. #pragma mark cancel order
  8546. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8547. {
  8548. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8549. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8550. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8551. if (order_id.length) {
  8552. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8553. }
  8554. int ret = [iSalesDB execSql:sql];
  8555. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8556. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8557. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8558. // [dic setValue:@"160409" forKey:@"min_ver"];
  8559. return [RAConvertor dict2data:dic];
  8560. }
  8561. #pragma mark sign order
  8562. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8563. {
  8564. //参考 offline_saveBusinesscard
  8565. DebugLog(@"sign order params: %@",params);
  8566. // orderCode = MOB1608240002;
  8567. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8568. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8569. orderCode = [self translateSingleQuote:orderCode];
  8570. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8571. picPath = [self translateSingleQuote:picPath];
  8572. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8573. int ret = [iSalesDB execSql:sql];
  8574. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8575. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8576. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8577. // [dic setValue:@"160409" forKey:@"min_ver"];
  8578. return [RAConvertor dict2data:dic];
  8579. }
  8580. #pragma mark save order
  8581. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8582. NSString *ret = [self valueInParams:params key:key];
  8583. if (translate) {
  8584. ret = [self translateSingleQuote:ret];
  8585. }
  8586. return ret;
  8587. }
  8588. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8589. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8590. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8591. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8592. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8593. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8594. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8595. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8596. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8597. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8598. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8599. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8600. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8601. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8602. if (![total_price isEqualToString:@""]) {
  8603. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8604. } else {
  8605. total_price = @"";
  8606. }
  8607. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8608. if ([paymentsAndCredits isEqualToString:@""]) {
  8609. paymentsAndCredits = @"";
  8610. } else {
  8611. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8612. }
  8613. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8614. if ([handling_fee_value isEqualToString:@""]) {
  8615. handling_fee_value = @"";
  8616. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8617. handling_fee_value = @"";
  8618. } else {
  8619. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8620. }
  8621. NSString *handling_fee_placeholder = handling_fee_value;
  8622. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8623. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8624. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8625. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8626. NSString *lift_gate_placeholder = @"";
  8627. if ([lift_gate_value isEqualToString:@""]) {
  8628. lift_gate_placeholder = @"";
  8629. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8630. lift_gate_placeholder = @"";
  8631. } else {
  8632. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8633. }
  8634. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8635. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8636. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8637. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8638. 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];
  8639. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8640. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8641. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8642. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8643. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8644. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8645. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8646. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8647. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8648. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8649. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8650. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8651. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8652. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8653. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8654. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8655. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8656. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8657. if (!number_nil) {
  8658. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8659. }
  8660. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8661. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8662. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8663. if (!security_code_nil) {
  8664. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8665. }
  8666. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8667. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8668. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8669. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8670. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8671. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8672. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8673. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8674. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8675. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8676. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8677. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8678. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8679. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8680. NSString *contact_id = customer_cid;
  8681. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8682. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8683. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8684. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8685. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8686. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8687. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8688. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8689. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8690. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8691. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8692. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8693. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8694. //
  8695. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8696. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8697. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8698. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8699. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8700. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8701. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8702. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8703. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8704. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8705. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8706. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8707. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8708. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8709. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8710. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8711. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8712. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8713. 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];
  8714. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8715. if (receive_cid.length) {
  8716. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8717. }
  8718. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8719. if (receive_name.length) {
  8720. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8721. }
  8722. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8723. if (receive_ext.length) {
  8724. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8725. }
  8726. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8727. if (receive_contact.length) {
  8728. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8729. }
  8730. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8731. if (receive_phone.length) {
  8732. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8733. }
  8734. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8735. if (receive_email.length) {
  8736. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8737. }
  8738. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8739. if (receive_fax.length) {
  8740. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8741. }
  8742. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8743. if (sender_cid.length) {
  8744. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8745. }
  8746. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8747. if (sender_name.length) {
  8748. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8749. }
  8750. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8751. if (sender_ext.length) {
  8752. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8753. }
  8754. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8755. if(sender_contact.length) {
  8756. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8757. }
  8758. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8759. if (sender_phone.length) {
  8760. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8761. }
  8762. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8763. if (sender_fax.length) {
  8764. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8765. }
  8766. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8767. if (sender_email.length) {
  8768. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8769. }
  8770. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8771. if (shipping_billto_cid.length) {
  8772. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8773. }
  8774. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8775. if (shipping_billto_name.length) {
  8776. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8777. }
  8778. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8779. if (shipping_billto_ext.length) {
  8780. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8781. }
  8782. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8783. if (shipping_billto_contact.length) {
  8784. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8785. }
  8786. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8787. if (shipping_billto_phone.length) {
  8788. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8789. }
  8790. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8791. if (shipping_billto_fax.length) {
  8792. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8793. }
  8794. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8795. if (shipping_billto_email.length) {
  8796. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8797. }
  8798. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8799. if (billing_cid.length) {
  8800. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8801. }
  8802. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8803. if (billing_name.length) {
  8804. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8805. }
  8806. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8807. if (billing_ext.length) {
  8808. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8809. }
  8810. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8811. if (billing_contact.length) {
  8812. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8813. }
  8814. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8815. if (billing_phone.length) {
  8816. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8817. }
  8818. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8819. if (billing_fax.length) {
  8820. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8821. }
  8822. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8823. if (billing_email.length) {
  8824. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8825. }
  8826. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8827. if (returnto_cid.length) {
  8828. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8829. }
  8830. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8831. if (returnto_name.length) {
  8832. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8833. }
  8834. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8835. if (returnto_ext.length) {
  8836. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8837. }
  8838. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8839. if (returnto_contact.length) {
  8840. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8841. }
  8842. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8843. if (returnto_phone.length) {
  8844. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8845. }
  8846. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8847. if (returnto_fax.length) {
  8848. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8849. }
  8850. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8851. if (returnto_email.length) {
  8852. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8853. }
  8854. NSString *order_status = @"status = 1,";
  8855. if (submit) {
  8856. order_status = @"status = -11,";
  8857. }
  8858. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8859. NSString *sync_sql = @"";
  8860. if (submit) {
  8861. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8862. [param setValue:sales_rep forKey:@"sales_rep"];
  8863. NSString *sync_data = [self translateSingleQuote:[RAConvertor dict2string:param]];
  8864. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8865. }
  8866. 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];
  8867. DebugLog(@"save order contactSql: %@",contactSql);
  8868. DebugLog(@"save order orderSql: %@",orderSql);
  8869. // int contact_ret = [iSalesDB execSql:contactSql];
  8870. int order_ret = [iSalesDB execSql:orderSql];
  8871. int ret = order_ret;
  8872. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8873. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8874. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8875. // [dic setValue:@"160409" forKey:@"min_ver"];
  8876. [dic setObject:so_id forKey:@"so#"];
  8877. return [RAConvertor dict2data:dic];
  8878. }
  8879. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8880. {
  8881. // id foo = nil;
  8882. // NSMutableArray *a = @[].mutableCopy;
  8883. // [a addObject:foo];
  8884. return [self saveorder:param submit:NO];
  8885. }
  8886. #pragma mark add to cart by name
  8887. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8888. {
  8889. NSString *orderCode = [params objectForKey:@"orderCode"];
  8890. NSString *product_name = [params objectForKey:@"product_name"];
  8891. product_name = [self translateSingleQuote:product_name];
  8892. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8893. sqlite3 *db = [iSalesDB get_db];
  8894. __block NSMutableString *product_id_string = [NSMutableString string];
  8895. for (int i = 0; i < product_name_array.count; i++) {
  8896. NSString *name = [product_name_array objectAtIndex:i];
  8897. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8898. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8899. int product_id = sqlite3_column_int(stmt, 0);
  8900. if (i == product_name_array.count - 1) {
  8901. [product_id_string appendFormat:@"%d",product_id];
  8902. } else {
  8903. [product_id_string appendFormat:@"%d,",product_id];
  8904. }
  8905. }];
  8906. }
  8907. NSDictionary *newParams = @{
  8908. @"product_id" : product_id_string,
  8909. @"orderCode" : orderCode
  8910. };
  8911. [iSalesDB close_db:db];
  8912. return [self offline_add2cart:[newParams mutableCopy]];
  8913. }
  8914. #pragma mark reset order
  8915. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8916. {
  8917. UIApplication * app = [UIApplication sharedApplication];
  8918. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8919. appDelegate.disable_trigger=true;
  8920. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  8921. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8922. appDelegate.disable_trigger=false;
  8923. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8924. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8925. return [RAConvertor dict2data:dic];
  8926. }
  8927. #pragma mark submit order
  8928. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8929. {
  8930. return [self saveorder:params submit:YES];
  8931. }
  8932. #pragma mark copy order
  8933. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8934. {
  8935. NSMutableDictionary *ret = @{}.mutableCopy;
  8936. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8937. sqlite3 *db = [iSalesDB get_db];
  8938. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8939. // 首先查看联系人是否active
  8940. 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];
  8941. __block int customer_is_active = 1;
  8942. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8943. customer_is_active = sqlite3_column_int(stmt, 0);
  8944. }];
  8945. if (!customer_is_active) {
  8946. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8947. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8948. [iSalesDB close_db:db];
  8949. return [RAConvertor dict2data:ret];
  8950. }
  8951. // new order
  8952. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8953. NSString *new_order_code = [self get_offline_soid:db];
  8954. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8955. user = [self translateSingleQuote:user];
  8956. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,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,sales_terms) select '%@',1,submit_as,internal_notes,sales_rep,'%@',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,sales_terms from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  8957. __block int result = 1;
  8958. result = [iSalesDB execSql:insert_order_sql db:db];
  8959. if (!result) {
  8960. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8961. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8962. [iSalesDB close_db:db];
  8963. return [RAConvertor dict2data:ret];
  8964. }
  8965. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8966. // __block NSString *product_id = @"";
  8967. __weak typeof(self) weakSelf = self;
  8968. 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];
  8969. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8970. int is_active = sqlite3_column_int(stmt, 1);
  8971. if (is_active) {
  8972. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8973. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8974. 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];
  8975. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8976. }
  8977. }];
  8978. // product_id = [product_id substringFromIndex:1];
  8979. //
  8980. // [self offline_add2cart:@{}.mutableCopy];
  8981. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8982. [iSalesDB close_db:db];
  8983. if (result) {
  8984. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8985. } else {
  8986. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8987. }
  8988. [ret setObject:new_order_code forKey:@"so_id"];
  8989. return [RAConvertor dict2data:ret];
  8990. }
  8991. #pragma mark move wish list to cart
  8992. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8993. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  8994. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8995. NSString *collectId = params[@"collectId"];
  8996. NSString *sql = [NSString stringWithFormat:@"select w.product_id,w.qty,p.availability,w._id from wishlist as w left join product as p on w.product_id = p.product_id where w._id in (%@);",collectId];
  8997. __block NSString *product_id = @"";
  8998. __block NSString *qty = @"";
  8999. __block int number_of_outOfStock = 0;
  9000. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  9001. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9002. int productId = sqlite3_column_int(stmt, 0);
  9003. int item_qty = sqlite3_column_int(stmt, 1);
  9004. int availability = sqlite3_column_int(stmt, 2);
  9005. int _id = sqlite3_column_int(stmt, 3);
  9006. if (!appDelegate.can_create_backorder) {
  9007. // 库存小于购买量为缺货
  9008. if (availability >= item_qty) {
  9009. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9010. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9011. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9012. } else {
  9013. number_of_outOfStock++;
  9014. }
  9015. } else {
  9016. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  9017. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  9018. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  9019. }
  9020. }];
  9021. NSMutableDictionary *retDic = nil;
  9022. if (!appDelegate.can_create_backorder) {
  9023. if (delete_collectId.count == 0) {
  9024. retDic = [NSMutableDictionary dictionary];
  9025. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9026. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9027. return [RAConvertor dict2data:retDic];
  9028. }
  9029. }
  9030. if (product_id.length > 1) {
  9031. product_id = [product_id substringFromIndex:1];
  9032. }
  9033. if (qty.length > 1) {
  9034. qty = [qty substringFromIndex:1];
  9035. }
  9036. NSDictionary *newParams = @{
  9037. @"product_id" : product_id,
  9038. @"orderCode" : params[@"orderCode"],
  9039. @"qty" : qty
  9040. };
  9041. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  9042. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  9043. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  9044. NSInteger ret = [[[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] valueForKey:@"result"] integerValue];
  9045. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  9046. }
  9047. if (!appDelegate.can_create_backorder) {
  9048. if (number_of_outOfStock > 0) {
  9049. retDic[@"result"] = [NSNumber numberWithInteger:8];
  9050. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  9051. }
  9052. }
  9053. return [RAConvertor dict2data:retDic];
  9054. }
  9055. #pragma mark - portfolio
  9056. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  9057. int sort = [[params valueForKey:@"sort"] intValue];
  9058. int offset = [[params valueForKey:@"offset"] intValue];
  9059. int limit = [[params valueForKey:@"limit"] intValue];
  9060. NSString *orderBy = @"";
  9061. switch (sort) {
  9062. case 0:{
  9063. orderBy = @"p.modify_time desc";
  9064. }
  9065. break;
  9066. case 1:{
  9067. orderBy = @"modify_time asc";
  9068. }
  9069. break;
  9070. case 2:{
  9071. orderBy = @"p.name asc";
  9072. }
  9073. break;
  9074. case 3:{
  9075. orderBy = @"p.name desc";
  9076. }
  9077. break;
  9078. case 4:{
  9079. orderBy = @"p.description asc";
  9080. }
  9081. break;
  9082. case 5: {
  9083. orderBy = @"m.default_category asc";
  9084. }
  9085. default:
  9086. break;
  9087. }
  9088. // NSString *sql = [NSString stringWithFormat:@"select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  9089. NSString *sql = [NSString stringWithFormat:@"select * from (select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent,p.modify_time from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id limit %d offset %d) as p left join product as m on p.product_id = m.product_id order by %@;",limit,offset,orderBy];
  9090. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9091. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9092. sqlite3 *db = [iSalesDB get_db];
  9093. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9094. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9095. int product_id = sqlite3_column_int(stmt, 0);
  9096. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9097. NSString *name = [self textAtColumn:1 statement:stmt];
  9098. NSString *description = [self textAtColumn:2 statement:stmt];
  9099. double price = sqlite3_column_double(stmt, 3);
  9100. double discount = sqlite3_column_double(stmt,4);
  9101. int qty = sqlite3_column_int(stmt, 5);
  9102. int percentage = sqlite3_column_int(stmt, 6);
  9103. int item_id = sqlite3_column_int(stmt, 7);
  9104. // int fashion_id = sqlite3_column_int(stmt, 8);
  9105. NSString *img_path = [self textAtColumn:9 statement:stmt];
  9106. NSString *line_note = [self textAtColumn:10 statement:stmt];
  9107. double percent = sqlite3_column_double(stmt, 11);
  9108. NSString *price_null = [self textAtColumn:3 statement:stmt];
  9109. if ([price_null isEqualToString:@"null"]) {
  9110. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9111. }
  9112. NSMutableDictionary *item = @{
  9113. @"linenotes": line_note,
  9114. @"check": @(1),
  9115. @"product_id": product_id_string,
  9116. @"available_qty": @(qty),
  9117. @"available_percent" : @(percent),
  9118. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  9119. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9120. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  9121. @"img": img_path,
  9122. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  9123. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  9124. }.mutableCopy;
  9125. if (percentage) {
  9126. [item removeObjectForKey:@"available_qty"];
  9127. } else {
  9128. [item removeObjectForKey:@"available_percent"];
  9129. }
  9130. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  9131. if ([qty_null isEqualToString:@"null"]) {
  9132. [item removeObjectForKey:@"available_qty"];
  9133. }
  9134. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9135. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9136. }];
  9137. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9138. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9139. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9140. }
  9141. [iSalesDB close_db:db];
  9142. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  9143. [dic setValue:@"" forKey:@"email_content"];
  9144. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  9145. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9146. return [RAConvertor dict2data:dic];
  9147. }
  9148. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  9149. __block int qty = 0;
  9150. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9151. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9152. qty = sqlite3_column_int(stmt, 0);
  9153. }];
  9154. return qty;
  9155. }
  9156. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  9157. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  9158. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  9159. NSMutableDictionary * values = params[@"replaceValue"];
  9160. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  9161. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  9162. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  9163. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  9164. NSString *pdf_path = @"";
  9165. if (direct) {
  9166. configureParams = [self valueInParams:params key:@"pdfUrl"];
  9167. } else {
  9168. pdf_path = [self valueInParams:params key:@"pdfPath"];
  9169. }
  9170. NSString *create_user = [self valueInParams:params key:@"user"];
  9171. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  9172. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  9173. // model info
  9174. // NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  9175. // V1.90 more color
  9176. NSString *sql = [NSString stringWithFormat:@"select p.product_id,ifnull(p.sheet_price,'null'),ifnull(p.sheet_discount,'null'),ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.line_note,p.percent,m.more_color from offline_portfolio as p left join product as m on p.product_id = m.product_id where p.product_id in (%@);",product_ids];
  9177. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9178. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  9179. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9180. sqlite3 *db = [iSalesDB get_db];
  9181. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9182. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9183. int product_id = sqlite3_column_int(stmt, 0);
  9184. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  9185. double price = sqlite3_column_double(stmt, 1);
  9186. double discount = sqlite3_column_double(stmt,2);
  9187. int qty = sqlite3_column_int(stmt, 3);
  9188. int percentage = sqlite3_column_int(stmt, 4);
  9189. int item_id = sqlite3_column_int(stmt, 5);
  9190. NSString *line_note = [self textAtColumn:6 statement:stmt];
  9191. double percent = sqlite3_column_double(stmt, 7);
  9192. int more_color = sqlite3_column_int(stmt, 8);
  9193. NSString *price_null = [self textAtColumn:1 statement:stmt];
  9194. /* if ([price_null isEqualToString:@"null"]) {
  9195. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9196. }
  9197. */
  9198. [product_ids_string appendFormat:@"%@,",product_id_string];
  9199. // Regular Price
  9200. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  9201. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  9202. // QTY
  9203. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  9204. if ([qty_null isEqualToString:@"null"]) {
  9205. // 查available
  9206. qty = [self model_QTY:product_id_string db:db];
  9207. }
  9208. if (percentage) {
  9209. qty = qty * percent / 100;
  9210. }
  9211. // Special Price
  9212. if ([price_null isEqualToString:@"null"]) {
  9213. // price = regular price
  9214. price = [regular_price_str doubleValue];
  9215. }
  9216. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  9217. if (![discount_null isEqualToString:@"null"]) {
  9218. price = price * (1 - discount / 100.0);
  9219. }
  9220. NSMutableDictionary *item = @{
  9221. @"line_note": line_note,
  9222. @"product_id": product_id_string,
  9223. @"available_qty": @(qty),
  9224. @"more_color":@(more_color),
  9225. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  9226. @"regular_price" : regular_price_str,
  9227. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  9228. }.mutableCopy;
  9229. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9230. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9231. }];
  9232. [iSalesDB close_db:db];
  9233. if (product_ids_string.length > 0) {
  9234. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  9235. }
  9236. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  9237. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  9238. return [RAConvertor dict2data:resultDictionary];
  9239. }
  9240. NSString *model_info = [RAConvertor dict2string:dic];
  9241. // 创建PDF
  9242. // 在preview情况下保存,则不需要新建了
  9243. if (direct) {
  9244. NSMutableDictionary *tear_sheet_params = params;
  9245. // if (tear_sheet_id) {
  9246. // tear_sheet_params = values;
  9247. // }
  9248. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  9249. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  9250. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  9251. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  9252. resultDictionary = pdfInfo.mutableCopy;
  9253. } else { // 创建PDF失败
  9254. return pdfData;
  9255. }
  9256. } else {
  9257. // pdf_path 就是本地路径
  9258. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  9259. }
  9260. // 将文件移动到PDF Cache文件夹
  9261. NSString *newPath = [pdf_path lastPathComponent];
  9262. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9263. NSString *cachefolder = [paths objectAtIndex:0];
  9264. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9265. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  9266. NSFileManager *fileManager = [NSFileManager defaultManager];
  9267. NSError *error = nil;
  9268. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  9269. if (error) { // 移动文件失败
  9270. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9271. return [RAConvertor dict2data:resultDictionary];
  9272. }
  9273. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  9274. pdf_path = [newPath lastPathComponent];
  9275. // 保存信息
  9276. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  9277. NSString *off_params = [RAConvertor dict2string:params];
  9278. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  9279. pdf_path = [self translateSingleQuote:pdf_path];
  9280. create_user = [self translateSingleQuote:create_user];
  9281. tear_note = [self translateSingleQuote:tear_note];
  9282. tear_name = [self translateSingleQuote:tear_name];
  9283. model_info = [self translateSingleQuote:model_info];
  9284. configureParams = [self translateSingleQuote:configureParams];
  9285. off_params = [self translateSingleQuote:off_params];
  9286. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ('%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  9287. if (tear_sheet_id) {
  9288. int _id = [tear_sheet_id integerValue];
  9289. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  9290. }
  9291. int result = [iSalesDB execSql:save_pdf_sql];
  9292. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9293. //
  9294. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  9295. if (remove_Item) {
  9296. // portfolioId
  9297. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  9298. }
  9299. return [RAConvertor dict2data:resultDictionary];
  9300. }
  9301. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  9302. return [self offline_savePDF:params direct:YES];
  9303. }
  9304. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  9305. return [self offline_savePDF:params direct:NO];
  9306. }
  9307. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  9308. int offset = [[params valueForKey:@"offset"] integerValue];
  9309. int limit = [[params valueForKey:@"limit"] integerValue];
  9310. NSString *keyword = [params valueForKey:@"keyWord"];
  9311. NSString *where = @"where is_delete is null or is_delete = 0";
  9312. if (keyword.length) {
  9313. keyword = [self translateSingleQuote:keyword];
  9314. where = [NSString stringWithFormat:@"where (is_delete is null or is_delete = 0 ) and lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  9315. }
  9316. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  9317. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9318. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9319. NSString *cachefolder = [paths objectAtIndex:0];
  9320. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9321. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9322. NSString *name = [self textAtColumn:0 statement:stmt];
  9323. NSString *note = [self textAtColumn:1 statement:stmt];
  9324. NSString *time = [self textAtColumn:2 statement:stmt];
  9325. NSString *user = [self textAtColumn:3 statement:stmt];
  9326. NSString *path = [self textAtColumn:4 statement:stmt];
  9327. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9328. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9329. path = [pdfFolder stringByAppendingPathComponent:path];
  9330. BOOL bdir=NO;
  9331. NSFileManager* fileManager = [NSFileManager defaultManager];
  9332. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9333. {
  9334. //pdf文件不存在
  9335. path=nil;
  9336. }
  9337. time = [self changeDateTimeFormate:time];
  9338. time = [time stringByAppendingString:@" PST"];
  9339. int sheet_id = sqlite3_column_int(stmt, 5);
  9340. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9341. item[@"tearsheetsId"]=@(sheet_id);
  9342. item[@"pdf_path"]=path;
  9343. item[@"create_time"]=time;
  9344. item[@"create_user"]=user;
  9345. item[@"tear_note"]=note;
  9346. item[@"tear_name"]=name;
  9347. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9348. item[@"model_info"]=model_info;
  9349. item[@"off_params"]=off_params;
  9350. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9351. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9352. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9353. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9354. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9355. }];
  9356. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9357. return [RAConvertor dict2data:dic];
  9358. }
  9359. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9360. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9361. // NSString *user = [params objectForKey:@"user"];
  9362. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  9363. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  9364. int result = [iSalesDB execSql:sql];
  9365. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9366. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9367. if (result == RESULT_TRUE) {
  9368. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9369. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9370. sqlite3 *db = [iSalesDB get_db];
  9371. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9372. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9373. [iSalesDB close_db:db];
  9374. }
  9375. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9376. return [RAConvertor dict2data:dic];
  9377. }
  9378. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9379. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9380. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9381. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9382. NSMutableDictionary *section_0 = [[dic objectForKey:@"section_0"] mutableCopy];
  9383. NSDictionary *company_item = @{
  9384. @"control": @"text",
  9385. @"keyboard": @"text",
  9386. @"name": @"company_name",
  9387. @"value": COMPANY_FULL_NAME,
  9388. @"aname": @"Company Name"
  9389. };
  9390. [section_0 setObject:company_item forKey:@"item_0"];
  9391. [dic setObject:section_0 forKey:@"section_0"];
  9392. // Regurlar Price
  9393. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9394. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9395. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9396. __block long val_count = 0;
  9397. NSString *sql = @"select name,type,order_by from price order by order_by";
  9398. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9399. NSString *name = [self textAtColumn:0 statement:stmt];
  9400. int type = sqlite3_column_int(stmt, 1);
  9401. int order_by = sqlite3_column_int(stmt, 2);
  9402. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9403. NSDictionary *price_dic = @{
  9404. @"value" : name,
  9405. @"value_id" : [NSNumber numberWithInteger:type],
  9406. @"check" : order_by == 0 ? @(1) : @(0)
  9407. };
  9408. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9409. val_count = ++(*count);
  9410. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9411. val_count = 0;
  9412. }];
  9413. [cadedate setObject:@{@"value" : @"None",
  9414. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9415. val_count++;
  9416. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9417. [price setObject:cadedate forKey:@"cadedate"];
  9418. [section1 setObject:price forKey:@"item_2"];
  9419. [dic setObject:section1 forKey:@"section_1"];
  9420. return [RAConvertor dict2data:dic];
  9421. }
  9422. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9423. NSString *product_id = [params objectForKey:@"fashionId"];
  9424. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9425. __block int result = RESULT_TRUE;
  9426. __block int qty = 0;
  9427. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9428. qty = sqlite3_column_int(stmt, 0);
  9429. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9430. result = RESULT_FALSE;
  9431. }];
  9432. NSMutableDictionary *dic = @{
  9433. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9434. @"mode" : @"Regular Mode",
  9435. @"quantity_available" : @(qty),
  9436. @"result" : @(result),
  9437. }.mutableCopy;
  9438. return [RAConvertor dict2data:dic];
  9439. }
  9440. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9441. NSString *item_ids = [params objectForKey:@"item_id"];
  9442. NSString *line_notes = [params objectForKey:@"notes"];
  9443. NSString *price_str = [params objectForKey:@"price"];
  9444. NSString *discount_str = [params objectForKey:@"discount"];
  9445. NSString *percent = [params objectForKey:@"available_percent"];
  9446. NSString *qty = [params objectForKey:@"available_qty"];
  9447. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9448. int dot = 0;
  9449. if (line_notes) {
  9450. line_notes = [self translateSingleQuote:line_notes];
  9451. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9452. sql = [sql stringByAppendingString:line_notes];
  9453. dot = 1;
  9454. } else {
  9455. line_notes = @"";
  9456. }
  9457. if (price_str) {
  9458. if (dot) {
  9459. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9460. } else {
  9461. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9462. dot = 1;
  9463. }
  9464. sql = [sql stringByAppendingString:price_str];
  9465. } else {
  9466. price_str = @"";
  9467. }
  9468. if (discount_str) {
  9469. if (dot) {
  9470. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9471. } else {
  9472. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9473. dot = 1;
  9474. }
  9475. sql = [sql stringByAppendingString:discount_str];
  9476. } else {
  9477. discount_str = @"";
  9478. }
  9479. if (percent) {
  9480. if (dot) {
  9481. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9482. } else {
  9483. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9484. dot = 1;
  9485. }
  9486. sql = [sql stringByAppendingString:percent];
  9487. } else {
  9488. percent = @"";
  9489. }
  9490. if (qty) {
  9491. if (dot) {
  9492. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9493. } else {
  9494. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9495. dot = 1;
  9496. }
  9497. sql = [sql stringByAppendingString:qty];
  9498. } else {
  9499. qty = @"";
  9500. }
  9501. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9502. sql = [sql stringByAppendingString:where];
  9503. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9504. int result = [iSalesDB execSql:sql];
  9505. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9506. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9507. return [RAConvertor dict2data:dic];
  9508. }
  9509. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9510. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9511. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9512. sqlite3 *db = [iSalesDB get_db];
  9513. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  9514. int result = [iSalesDB execSql:sql db:db];
  9515. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9516. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9517. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9518. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9519. [iSalesDB close_db:db];
  9520. return [RAConvertor dict2data:dic];
  9521. }
  9522. + (void)offline_removePDFWithName:(NSString *)name {
  9523. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9524. NSString *cachefolder = [paths objectAtIndex:0];
  9525. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9526. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9527. NSFileManager *fileManager = [NSFileManager defaultManager];
  9528. [fileManager removeItemAtPath:path error:nil];
  9529. }
  9530. + (void)offline_clear_PDFCache {
  9531. NSFileManager *fileManager = [NSFileManager defaultManager];
  9532. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9533. NSString *cachefolder = [paths objectAtIndex:0];
  9534. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9535. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9536. for (NSString *path in pdf_files) {
  9537. [self offline_removePDFWithName:[path lastPathComponent]];
  9538. }
  9539. }
  9540. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9541. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9542. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9543. NSString *user = [params objectForKey:@"user"];
  9544. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  9545. if (![create_user isEqualToString:user]) {
  9546. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9547. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9548. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  9549. return [RAConvertor dict2data:dic];
  9550. }
  9551. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9552. __block int is_local = 0;
  9553. __block NSString *path = @"";
  9554. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9555. is_local = sqlite3_column_int(stmt, 0);
  9556. path = [self textAtColumn:1 statement:stmt];
  9557. }];
  9558. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9559. if (is_local == 1) {
  9560. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9561. // 删除文件
  9562. [self offline_removePDFWithName:path];
  9563. }
  9564. int result = [iSalesDB execSql:sql];
  9565. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9566. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9567. return [RAConvertor dict2data:dic];
  9568. }
  9569. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  9570. {
  9571. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9572. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9573. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9574. NSString* where=@"1=1";
  9575. if (ver!=nil) {
  9576. where=@"is_dirty=1";
  9577. }
  9578. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  9579. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9580. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9581. sqlite3 *db = [iSalesDB get_db];
  9582. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9583. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9584. NSInteger _id = sqlite3_column_int(stmt, 0);
  9585. NSInteger product_id = sqlite3_column_int(stmt, 1);
  9586. NSString *name = [self textAtColumn:2 statement:stmt];
  9587. NSString *desc = [self textAtColumn:3 statement:stmt];
  9588. NSInteger item_id = sqlite3_column_int(stmt, 4);
  9589. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  9590. NSInteger qty = sqlite3_column_int(stmt, 6);
  9591. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9592. double percent = sqlite3_column_double(stmt, 8);
  9593. double price = sqlite3_column_double(stmt, 9);
  9594. double discount = sqlite3_column_double(stmt, 10);
  9595. NSString *img = [self textAtColumn:11 statement:stmt];
  9596. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9597. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9598. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9599. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9600. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9601. if ([price_null isEqualToString:@"null"]) {
  9602. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9603. }
  9604. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9605. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9606. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9607. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9608. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9609. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9610. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9611. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9612. if ([qty_null isEqualToString:@"null"]) {
  9613. // [item setValue:@"null" forKey:@"available_qty"];
  9614. } else {
  9615. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9616. }
  9617. if ([is_percent_null isEqualToString:@"null"]) {
  9618. // [item setValue:@"null" forKey:@"percentage"];
  9619. } else {
  9620. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9621. }
  9622. if ([percent_null isEqualToString:@"null"]) {
  9623. // [item setValue:@"null" forKey:@"percent"];
  9624. } else {
  9625. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9626. }
  9627. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9628. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9629. [item setValue:name forKey:@"name"];
  9630. [item setValue:desc forKey:@"description"];
  9631. [item setValue:img forKey:@"img"];
  9632. [item setValue:line_note forKey:@"line_note"];
  9633. [item setValue:create_time forKey:@"createtime"];
  9634. [item setValue:modify_time forKey:@"modifytime"];
  9635. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9636. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9637. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9638. }];
  9639. [iSalesDB close_db:db];
  9640. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9641. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9642. }
  9643. return ret;
  9644. }
  9645. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9646. {
  9647. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9648. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9649. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9650. NSString* where=@"1=1";
  9651. if (ver!=nil) {
  9652. where=@"is_dirty=1";
  9653. }
  9654. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,ifnull(tearsheets_id,'null'),pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params,is_delete,is_local from offline_pdf where %@",where];
  9655. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9656. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9657. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9658. NSInteger _id = sqlite3_column_int(stmt, 0);
  9659. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9660. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9661. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9662. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9663. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9664. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9665. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9666. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9667. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9668. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9669. NSString *uuid = [NSUUID UUID].UUIDString;
  9670. int is_delete = sqlite3_column_int(stmt, 11);
  9671. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9672. int is_local = sqlite3_column_int(stmt, 12);
  9673. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9674. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9675. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9676. }
  9677. [item setObject:pdf_path forKey:@"pdf_path"];
  9678. [item setObject:create_user forKey:@"create_user"];
  9679. [item setObject:tear_note forKey:@"tear_note"];
  9680. [item setObject:tear_name forKey:@"tear_name"];
  9681. [item setObject:model_info forKey:@"model_info"];
  9682. [item setObject:createtime forKey:@"createtime"];
  9683. [item setObject:modifytime forKey:@"modifytime"];
  9684. [item setObject:urlParams forKey:@"urlParams"];
  9685. [item setObject:off_params forKey:@"off_params"];
  9686. [item setObject:uuid forKey:@"pdf_token"];
  9687. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9688. NSDictionary *off_Params_dic = [RAConvertor string2dict:off_params];
  9689. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9690. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9691. } else {
  9692. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9693. }
  9694. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9695. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9696. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9697. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9698. }];
  9699. return ret;
  9700. }
  9701. @end